第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 tweepy 中使用 Cursor 處理速率限制異常

在 tweepy 中使用 Cursor 處理速率限制異常

函數(shù)式編程 2022-06-02 16:28:01
在使用 Cursor 對象遍歷關(guān)注者列表時,我試圖找到處理速率限制的正確方法。這是我正在嘗試的:while True:    try:        for follower in tweepy.Cursor(api.followers, id=root_usr).items():            print(follower.id)    except tweepy.TweepError:        # hit rate limit, sleep for 15 minutes        print('Rate limited. Sleeping for 15 minutes.')        time.sleep(15 * 60 + 15)        continue    except StopIteration:        break這可能是不正確的,因為異常會使for循環(huán)重新從頭開始。root_usr在處理速率限制問題的同時迭代所有關(guān)注者的正確方法是什么?
查看完整描述

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)用。


查看完整回答
反對 回復(fù) 2022-06-02
?
慕沐林林

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


查看完整回答
反對 回復(fù) 2022-06-02
  • 2 回答
  • 0 關(guān)注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號