[英]Dealing with Twitter Rate Limit
我有一個看似簡單的問題,正在努力尋找解決方案。 我有約3,000條Tweet 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:i+n]
tweets = []
id = list(chunks(listOfTwitterIDs, 100))
for each in id:
tweets.append(api.statuses_lookup(each, map=true))
但是,這將超過Twitter的速率限制。 達到速率限制后,如何引入15分鍾的等待時間?
tweepy API有一個wait_on_rate_limit
參數,默認情況下將其設置為False
。
tweepy文檔的 “代碼片段”中提供了另一個使用游標處理速率限制的示例。
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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.