私有 InstagramApi
Private InstagramApi
我正在尝试获取某个用户的关注者并根据特定条件过滤他们,因为我想缩小值得与之互动的用户列表。如果我不包括 time.sleep(),我会得到一个 KeyError。这样我得到 429、500 等...我知道这意味着请求太多,但是没有办法避免这种情况还是我做错了?有更多 effective/pythonic 的方法吗?提前致谢。
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。 "fix" 您对此无能为力,因为问题不在于您的代码。
您可以在请求之间设置延迟以防止出现 429。万一你得到 429,你应该暂时不要尝试做任何事情(除了每隔几分钟检查一次块是否消失)。
我正在尝试获取某个用户的关注者并根据特定条件过滤他们,因为我想缩小值得与之互动的用户列表。如果我不包括 time.sleep(),我会得到一个 KeyError。这样我得到 429、500 等...我知道这意味着请求太多,但是没有办法避免这种情况还是我做错了?有更多 effective/pythonic 的方法吗?提前致谢。
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。 "fix" 您对此无能为力,因为问题不在于您的代码。
您可以在请求之间设置延迟以防止出现 429。万一你得到 429,你应该暂时不要尝试做任何事情(除了每隔几分钟检查一次块是否消失)。