在Internet连接断开几秒钟后,Tweepy游标将退出

时间:2018-06-06 14:39:27

标签: python tweepy

尝试从Twitter获取数据,我使用此代码:

api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)    
for tweet in tweepy.Cursor(api.search, q='#WorldCup -filter:retweets since:2017-6-5 lang:en', tweet_mode='extended').items():

     json.dump(tweet._json, file, sort_keys=True, indent=4)
     num += 1
     print "Writing to JSON tweet number : " + str(num)

效果很好,但问题是当互联网连接断开10秒并重新连接时,它将在60秒后停止并退出并出现错误:

Failed to send request: HTTPSConnectionPool(host='api.twitter.com', port=443): Read timed out. (read timeout=60)

如何在重新连接到互联网后将其更改为恢复?

1 个答案:

答案 0 :(得分:1)

您可以将代码放在这样的循环中:

import time

api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)

while True:
    try:
        for tweet in tweepy.Cursor(api.search, q='#WorldCup -filter:retweets since:2017-6-5 lang:en', tweet_mode='extended').items():
             json.dump(tweet._json, file, sort_keys=True, indent=4)
             num += 1
             print "Writing to JSON tweet number : " + str(num)
    except tweepy.error.TweepError as e:
        print(str(e))

    time.sleep(60) # next search in 60 seconds, either it worked or not