我正在嘗試創建一個 Twitter 用戶圖,為此我編寫了以下代碼:import operatorimport sysimport timefrom urllib.error import URLErrorfrom http.client import BadStatusLineimport jsonimport twitterfrom functools import partialfrom sys import maxsize as maxintimport itertoolsimport networkximport matplotlib.pyplot as pltG = networkx.Graph()# Code and function taken from the twitter cookbookdef oauth_login(): CONSUMER_KEY = 'xxxx' CONSUMER_SECRET = 'xxZD6r' OAUTH_TOKEN = 'xxNRYl' OAUTH_TOKEN_SECRET = 'xxHYJl' auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET) twitter_api = twitter.Twitter(auth=auth) return twitter_api# Code and function taken from the twitter cookbookdef make_twitter_request(twitter_api_func, max_errors=10, *args, **kw): # A nested helper function that handles common HTTPErrors. Return an updated # value for wait_period if the problem is a 500 level error. Block until the # rate limit is reset if it's a rate limiting issue (429 error). Returns None # for 401 and 404 errors, which requires special handling by the caller. def handle_twitter_http_error(e, wait_period=2, sleep_when_rate_limited=True): if wait_period > 3600: # Seconds print('Too many retries. Quitting.', file=sys.stderr) raise e if e.e.code == 401: print('Encountered 401 Error (Not Authorized)', file=sys.stderr) return None elif e.e.code == 404: print('Encountered 404 Error (Not Found)', file=sys.stderr) return None elif e.e.code == 429:但是我經常收到此錯誤,這使我的程序運行速度非常慢輸入的 ID:60784269 已獲取 60784269 的 5000 個朋友 ID 總數 已獲取 60784269 的 5000 個關注者 ID 總數 遇到 429 錯誤(超出速率限制) 15 分鐘后重試...ZzZ...有辦法解決這個問題嗎?讓代碼運行得更快?我已經閱讀了一些文件,但我仍然沒有任何清晰的圖片。任何幫助表示贊賞。
1 回答

湖上湖
TA貢獻2003條經驗 獲得超2個贊
公共 API 無法繞過速率限制。
盡管現在有一個 API v2,它也允許您獲取用戶并且不會在相同的速率限制下工作。
請注意,此解決方案將是臨時的,因為 Twitter 將在某個時候刪除對 API v1 的訪問。
您可以請求 Twitter 訪問高級/企業級別的 API,但您必須為此付費。
您可以在此處查看速率限制文檔:
添加回答
舉報
0/150
提交
取消