Python 处理推特费率限制
我有一个看似简单的问题,我正在努力寻找解决方案。我有一个约3000条推特ID的列表,我希望获得该用户的转发次数、喜欢次数和追随者数量 为此,我编写了以下代码:Python 处理推特费率限制,python,twitter,tweepy,ratelimit,Python,Twitter,Tweepy,Ratelimit,我有一个看似简单的问题,我正在努力寻找解决方案。我有一个约3000条推特ID的列表,我希望获得该用户的转发次数、喜欢次数和追随者数量 为此,我编写了以下代码: def chunks(l, n): # For item i in a range that is a length of l, for i in range(0, len(l), n): # Create an index range for l of n items: yield l[i
def chunks(l, n):
# For item i in a range that is a length of l,
for i in range(0, len(l), n):
# Create an index range for l of n items:
yield l[i:i+n]
tweets = []
id = list(chunks(listOfTwitterIDs, 100))
for each in id:
tweets.append(api.statuses_lookup(each, map=true))
然而,这将超过Twitter的费率限制。当我达到费率限制时,如何引入15分钟的等待时间?有一个wait\u on\u rate\u limit
参数,默认设置为False
代码片段中提供了使用游标处理速率限制的另一个示例。有一个等待速率限制
参数,默认设置为False
def chunks(l, n):
# For item i in a range that is a length of l,
for i in range(0, len(l), n):
# Create an index range for l of n items:
yield l[i:i+n]
tweets = []
id = list(chunks(listOfTwitterIDs, 100))
for each in id:
# try to get get # of retweets
try:
tweets.append(api.statuses_lookup(each, map=true))
# If it fails, wait 15 mins and 1 sec (just to be safe) and try again
except:
sleep(901)
tweets.append(api.statuses_lookup(each, map=true))
代码片段中提供了使用游标处理速率限制的另一个示例。time.sleep(15*60)time.sleep(15*60)
def chunks(l, n):
# For item i in a range that is a length of l,
for i in range(0, len(l), n):
# Create an index range for l of n items:
yield l[i:i+n]
tweets = []
id = list(chunks(listOfTwitterIDs, 100))
for each in id:
# try to get get # of retweets
try:
tweets.append(api.statuses_lookup(each, map=true))
# If it fails, wait 15 mins and 1 sec (just to be safe) and try again
except:
sleep(901)
tweets.append(api.statuses_lookup(each, map=true))