以下代码功能正常,它会将推文流式传输并存储在JSON文件中。我在虚拟机上运行它,因为我想收集2个月的数据。但是,出于某种原因,代码在大约48小时后停止运行且没有错误。这是一个tweepy限制(流媒体速率或其他什么)或我应该检查我的连接:
import os
import sys
from tweepy import API
from tweepy import OAuthHandler
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
def get_twitter_client():
"""Setup Twitter API client Return: tweepy.API object """
auth = get_twitter_auth()
client = API(auth)
return client
from tweepy import Stream
from tweepy.streaming import StreamListener
class MyListener(StreamListener):
def on_data(self, data):
try:
with open('strategyand_user.json', 'a') as f:
f.write(data)
return True
except BaseException as e:
print("Error on_data: %s" % str(e))
return True
def on_error(self, status):
print(status)
return True
twitter_stream = Stream(auth, MyListener())
twitter_stream.filter(follow=['xxxxxxx'])