Tweepy超时连接错误

时间:2017-12-09 05:40:43

标签: python-3.x twitter while-loop exception-handling tweepy

我阅读了有关此错误的任何内容: HTTPSConnectionPool(host =' api.twitter.com',port = 443):读取超时。 (读取超时= 60) 一旦错误显示脚本开始从第一页获取关注者。 它似乎在从twitter检索数据时出现连接错误时显示。任何想法都将非常感激,因为无法找到有关该问题的更多信息。我如何处理异常,以便我可以从停止的位置恢复光标而无需从第一页开始?我假设我需要保存光标对象并从那里恢复。

import tweepy
import csv
import time
from time import sleep
import datetime
from requests.exceptions import Timeout, ConnectionError
from requests.packages.urllib3.exceptions import ReadTimeoutError, ProtocolError

consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token = 'xxx'
access_token_secret = 'xxx'


auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)


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

csvFile = open('followers.csv', 'a')

csvWriter = csv.writer(csvFile)

while True:
try:
    for follower in tweepy.Cursor(api.followers, screen_name='twitterscreenname', count='200').items():
        print(follower.screen_name)
        csvWriter.writerow([follower.screen_name])
except tweepy.TweepError as e:
    print (e.reason)
    if 'Failed to send request:' in e.reason:
        print ('timeout error caught.')
        time.sleep(100)
        continue
except (Timeout, SSLError, ConnectionError, ReadTimeoutError, ProtocolError) as exc:
    print ('2nd exception')
    time.sleep(150)
    continue

0 个答案:

没有答案