【发布时间】:2017-02-06 22:08:26
【问题描述】:
以下是我的工作代码,用于获取某些帐户的 Twitter 关注者(在本例中为 @hudsonci)。
我的问题是吸引所有这些追随者所花费的时间。这个帐户特别有大约 1,000 名关注者……我一次只能达到 300 名,但有速率限制。因此,获得该帐户的所有关注者需要 > 一个小时。我可以想象这对于大客户来说将是一个巨大的痛苦。
我正在寻找一些关于如何改进这一点的建议。我觉得我没有充分利用分页光标,但我不能确定。
感谢任何帮助。
#!/usr/bin/env python
# encoding: utf-8
import tweepy
import time
#Twitter API credentials
consumer_key = "mine"
consumer_secret = "mine"
access_key = "mine"
access_secret = "mine"
#authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
def handle_errors(cursor):
while True:
try:
yield cursor.next()
except tweepy.TweepError:
time.sleep(20 * 60)
for user in handle_errors(tweepy.Cursor(api.followers,screen_name='hudsonci').items()):
print user.screen_name
【问题讨论】:
标签: python-2.7 twitter tweepy api-design rate-limiting