2 回答

TA貢獻1811條經(jīng)驗 獲得超6個贊
在文檔的代碼片段部分下有一個示例,建議使用包裝函數(shù)來處理錯誤處理。
def limit_handled(cursor):
while True:
try:
yield next(cursor)
except tweepy.RateLimitError:
time.sleep(15 * 60)
except StopIteration:
print("Done")
break
for follower in limit_handled(tweepy.Cursor(api.followers, id=root_usr).items()):
print(follower.id)
另外,我建議將 count 設(shè)置為最大值,tweepy.Cursor(api.followers, id=root_usr, count=200).items()以充分利用每個 API 調(diào)用。

TA貢獻2016條經(jīng)驗 獲得超9個贊
您可以在變量上構(gòu)造Cursor實例并next(cursor)在 try-catch 中調(diào)用。您可以這樣處理 StopIteration 和 TweepError。
喜歡
cursor = tweepy.Cursor(api.followers, id=root_usr).items()
# or tweepy.Cursor(api.followers, id=root_usr).items().iter()
while True:
try:
follower = next(cursor)
print(follower.id)
except StopIteration:
break
except tweepy.TweepError:
# Handle this, sleep, log, etc.
pass
添加回答
舉報