首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >私有InstagramApi

私有InstagramApi
EN

Stack Overflow用户
提问于 2018-04-19 17:38:37
回答 2查看 631关注 0票数 0

我试图得到某个用户的追随者并根据特定的标准过滤他们,因为我想缩小值得与之交互的用户列表。如果不包括time.sleep(),就会得到一个KeyError。这样我就能得到429,500等等.我知道这意味着太多的请求,但难道没有办法绕过这个问题吗?还是我做错了?有没有一种更有效的/仿生的方法来做到这一点?提前谢谢。

代码语言:javascript
复制
import imageio
imageio.plugins.ffmpeg.download()
from InstagramAPI import InstagramAPI
import re
import time

def get_user_followers(api, user_id):
    followers = []
    next_max_id = True
    while next_max_id:
        # first iteration hack
        if next_max_id is True:
            next_max_id = ''
        _ = api.getUserFollowers(user_id, maxid=next_max_id)
        followers.extend(api.LastJson.get('users', []))
        next_max_id = api.LastJson.get('next_max_id', '')
    return followers

#returns user id of the user you want to get followers from
def get_user_id(self, user):
    self.SendRequest('users/' + str(user) + '/usernameinfo/')
    userid = self.LastJson['user']['pk']
    return userid


follower_list = []
def scrape_followers(self, user):
    # gets the id from a user
    self.SendRequest('users/' + str(user) + '/usernameinfo/')
    userid = self.LastJson['user']['pk']
    self.SendRequest('users/' + str(userid) + '/info/')

    # filter users based on criteria
    pp = self.LastJson['user']['has_anonymous_profile_picture']             #has profile pic
    fer = self.LastJson['user']['follower_count']                           #number of followers
    fing = self.LastJson['user']['following_count']                         #number of followings
    bio = self.LastJson['user']['biography']                                #certain words in bio
    b = re.compile(r'free|shop|download', re.IGNORECASE).search(bio)
    business = self.LastJson['user']['is_business']                         #isn't a business profile
    story = self.LastJson['user']['has_highlight_reels']                    #is active/has a story 
    if not pp and 1500 > fer > 50 and 1000 > fing > 100 and not business and story and b == None:
        follower_list.append(userid)
    # return follower_list


# ACTUAL CALL
# Your login details
api = InstagramAPI("xxx", "xxx")
api.login()
#Call the function
user = 'GlitteryHell'
userid = get_user_id(api, user)
followers = get_user_followers(api, userid)
for i in range(10):
    user = list(followers[i].values())[1]
    try:
        scrape_followers(api, user)
        time.sleep(1)
    except KeyError:
        time.sleep(10)
print(follower_list)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-22 13:23:21

最近,Instagram不推荐一些API,并且更改了几个API的响应,所以现在您无法获得用户的配置文件图片和其他信息作为API响应的一部分。

后面跟着相关的API也不受欢迎。

你可以在这里读到更多关于它的内容。

https://www.instagram.com/developer/changelog/

此外,请求的数量也发生了巨大的变化。它从每小时每用户4000人减少到200人。

您可以添加更多的令牌来增加每小时发送的请求数量。

票数 0
EN

Stack Overflow用户

发布于 2018-04-22 18:35:38

Instagram有内置的限制,以防止滥用他们的API。您无法“修复”这个问题,因为问题不在您的代码中。

您可以在请求之间设置延迟,以防止获得429 d。如果您得到了429 d,您可能不应该尝试在一段时间内做任何事情(除了检查块是否每隔几分钟就消失一次)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49927060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档