是否有可能获得拥有超过一百万粉丝的帐户的完整关注者列表,例如麦当劳?
我使用Tweepy并按照代码:
c = tweepy.Cursor(api.followers_ids, id = 'McDonalds')
ids = []
for page in c.pages():
ids.append(page)
我也试试这个:
for id in c.items():
ids.append(id)
但我总是得到'超出速率限制'错误,并且只有5000个关注者ID。
答案 0 :(得分:38)
为了避免速率限制,您可以/应该在下一个关注页面请求之前等待。看起来很hacky,但有效:
import time
import tweepy
auth = tweepy.OAuthHandler(..., ...)
auth.set_access_token(..., ...)
api = tweepy.API(auth)
ids = []
for page in tweepy.Cursor(api.followers_ids, screen_name="McDonalds").pages():
ids.extend(page)
time.sleep(60)
print len(ids)
希望有所帮助。
答案 1 :(得分:17)
进行连接时使用速率限制参数。 api将在速率限制内自我控制。
睡眠暂停并不错,我使用它来模拟一个人并在一个时间范围内展开活动,并将api速率限制作为最终控制。
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True)
还添加try / except以捕获和控制错误。
示例代码 https://github.com/aspiringguru/twitterDataAnalyse/blob/master/sample_rate_limit_w_cursor.py
我将我的密钥放在外部文件中,以便于管理。
https://github.com/aspiringguru/twitterDataAnalyse/blob/master/keys.py
答案 2 :(得分:0)
alecxe的回答很好,但是没有人提到文档。 Twitter API documentation中包含回答问题的正确信息和解释。从文档中:
结果以5,000个用户ID的组给出,并且可以在后续请求中使用next_cursor值浏览结果的多个“页面”。
答案 3 :(得分:0)
我使用此代码,它可用于大量关注者: 有两个功能,一个用于在每个睡眠期后保存关注者ID,另一个用于获取列表: 有点想念,但我希望会有所帮助。
def save_followers_status(filename,foloowersid):
path='//content//drive//My Drive//Colab Notebooks//twitter//'+filename
if not (os.path.isfile(path+'_followers_status.csv')):
with open(path+'_followers_status.csv', 'wb') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',')
if len(foloowersid)>0:
print("save followers status of ", filename)
file = path + '_followers_status.csv'
# https: // stackoverflow.com / questions / 3348460 / csv - file - written -with-python - has - blank - lines - between - each - row
with open(file, mode='a', newline='') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for row in foloowersid:
writer.writerow(np.array(row))
csv_file.closed
def get_followers_id(person):
foloowersid = []
count=0
influencer=api.get_user( screen_name=person)
influencer_id=influencer.id
number_of_followers=influencer.followers_count
print("number of followers count : ",number_of_followers,'\n','user id : ',influencer_id)
status = tweepy.Cursor(api.followers_ids, screen_name=person, tweet_mode="extended").items()
for i in range(0,number_of_followers):
try:
user=next(status)
foloowersid.append([user])
count += 1
except tweepy.TweepError:
print('error limite of twiter sleep for 15 min')
timestamp = time.strftime("%d.%m.%Y %H:%M:%S", time.localtime())
print(timestamp)
if len(foloowersid)>0 :
print('the number get until this time :', count,'all folloers count is : ',number_of_followers)
foloowersid = np.array(str(foloowersid))
save_followers_status(person, foloowersid)
foloowersid = []
time.sleep(15*60)
next(status)
except :
print('end of foloowers ', count, 'all followers count is : ', number_of_followers)
foloowersid = np.array(str(foloowersid))
save_followers_status(person, foloowersid)
foloowersid = []
save_followers_status(person, foloowersid)
# foloowersid = np.array(map(str,foloowersid))
return foloowersid