[英]Accommodating Twitter's rate limit in Python's Twython
我正在使用Twython通過Twitter的REST API提取數據。 我希望代碼在達到Twitter速率限制時會根據需要自動休息,然后再次開始查詢。
這是代碼,其中包含Twitter ID列表,並將其關注者ID添加到列表中:
for user in first_ids:
try:
followers = twitter.get_followers_ids(user_id=user, count=600)
for individual in followers['ids']:
if individual not in ids:
ids.append(individual)
except TwythonRateLimitError as error:
remainder = float(twitter.get_lastfunction_header(header='x-rate-limit-reset')) - time.time()
time.sleep(remainder)
continue
運行它時,出現以下錯誤:“連接異常終止。錯誤10054:遠程主機強行關閉了現有連接”
錯誤是什么意思? 我想這與Twitter的速率限制有關-是否還有其他方法可以解決?
您在程序進入睡眠狀態時保持連接打開狀態,請嘗試手動將其關閉,然后在睡眠超時后重新連接。 就像是:
except TwythonRateLimitError as error:
remainder = float(twitter.get_lastfunction_header(header='x-rate-limit-reset')) - time.time()
twitter.disconnect()
time.sleep(remainder)
twitter = Twython(APP_KEY, APP_SECRET,OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
continue
如果您使用的是REST API,則可以使用相同的解決方案刪除該API,而不是使用.disconnect(),只需使用
del twitter
代替
twitter.disconnect()
我有同樣的問題,對我有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.