我试图获得某个用户的关注者,并根据一定的标准过滤他们,因为我想缩小值得与之交互的用户列表。如果我不包括时间。睡觉(),我收到一个键错误。这样我就能得到429500,等等。。。我知道这意味着太多的要求,但有没有办法绕过这一点,还是我做错了?有没有更有效的/Python式的方法?提前谢谢。在
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)
最近Instagram不赞成使用一些API,并改变了一些API的响应 所以现在你不能得到用户的个人资料图片和其他信息作为API响应的一部分。在
后跟和后跟相关API也已弃用。在
你可以在这里读到更多。在
https://www.instagram.com/developer/changelog/
而且,请求的数量也有很大的变化。 每用户每小时从4000减少到200。在
您可以添加更多身份验证令牌,以增加每小时可以发送的请求数。在
Instagram有内置的限制,以防止滥用其API。你不能做任何事情来“修复”这个问题,因为问题不是你的代码。在
你可以在请求之间设置一个延迟,以防止得到429'd。如果你得到了429'd,你可能暂时不做任何事情(除了每隔几分钟检查块是否消失)。在
相关问题 更多 >
编程相关推荐