我试图得到某个用户的追随者并根据特定的标准过滤他们,因为我想缩小值得与之交互的用户列表。如果不包括time.sleep(),就会得到一个KeyError。这样我就能得到429,500等等.我知道这意味着太多的请求,但难道没有办法绕过这个问题吗?还是我做错了?有没有一种更有效的/仿生的方法来做到这一点?提前谢谢。
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)发布于 2018-04-22 13:23:21
最近,Instagram不推荐一些API,并且更改了几个API的响应,所以现在您无法获得用户的配置文件图片和其他信息作为API响应的一部分。
后面跟着相关的API也不受欢迎。
你可以在这里读到更多关于它的内容。
https://www.instagram.com/developer/changelog/
此外,请求的数量也发生了巨大的变化。它从每小时每用户4000人减少到200人。
您可以添加更多的令牌来增加每小时发送的请求数量。
发布于 2018-04-22 18:35:38
Instagram有内置的限制,以防止滥用他们的API。您无法“修复”这个问题,因为问题不在您的代码中。
您可以在请求之间设置延迟,以防止获得429 d。如果您得到了429 d,您可能不应该尝试在一段时间内做任何事情(除了检查块是否每隔几分钟就消失一次)。
https://stackoverflow.com/questions/49927060
复制相似问题