我正在嘗試創(chuàng)建一個(gè) 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:但是我經(jīng)常收到此錯(cuò)誤,這使我的程序運(yùn)行速度非常慢輸入的 ID:60784269 已獲取 60784269 的 5000 個(gè)朋友 ID 總數(shù) 已獲取 60784269 的 5000 個(gè)關(guān)注者 ID 總數(shù) 遇到 429 錯(cuò)誤(超出速率限制) 15 分鐘后重試...ZzZ...有辦法解決這個(gè)問(wèn)題嗎?讓代碼運(yùn)行得更快?我已經(jīng)閱讀了一些文件,但我仍然沒(méi)有任何清晰的圖片。任何幫助表示贊賞。
1 回答
湖上湖
TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊
公共 API 無(wú)法繞過(guò)速率限制。
盡管現(xiàn)在有一個(gè) API v2,它也允許您獲取用戶并且不會(huì)在相同的速率限制下工作。
請(qǐng)注意,此解決方案將是臨時(shí)的,因?yàn)?Twitter 將在某個(gè)時(shí)候刪除對(duì) API v1 的訪問(wèn)。
您可以請(qǐng)求 Twitter 訪問(wèn)高級(jí)/企業(yè)級(jí)別的 API,但您必須為此付費(fèi)。
您可以在此處查看速率限制文檔:
添加回答
舉報(bào)
0/150
提交
取消
