我使用tweepy搜索API收集Twitter数据,以下代码在半天的时间内运行良好,然后始终出现socket.timeout错误。我确实编写了代码来捕获socket.timeout错误,但似乎并不是真正能够捕获该错误,有人帮我吗?谢谢
import tweepy
import datetime
import time
import socket
print('this is my twitter bot')
CONSUMER_KEY = 'aaa'
CONSUMER_SECRET = 'aaa'
ACCESS_KEY = 'aaa'
ACCESS_SECRET = 'aaa'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
print ('Start !')
while True:
for data in tweepy.Cursor(api.search,
q="*",
geocode="-37.81430919,144.96449399,10km",
since = "2019-01-13,",
until = "2019-01-26",
lang="en").items():
try:
with open("old.txt", 'a') as tf:
tf.write(str(data._json) + '\n')
except tweepy.error.TweepError:
print ('sleeping for 15 min')
time.sleep(60 * 15)
continue
except socket.timeout as e:
print(e)
print('socket time out, but continue')
continue
except StopIteration:
break
except:
continue
print ('done')