我正在尝试通过标签或任何研究标准从Twitter上获取所有可能的信息。对我来说,这是一项全新的活动。我在网上对此进行了修改,最后得到了类似的内容:
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import twitter_credentials # another file with the credentials
# # # # TWITTER STREAMER # # # #
class TwitterStreamer():
"""
Class for streaming and processing live tweets.
"""
def __init__(self):
pass
def stream_tweets(self, fetched_tweets_filename, hash_tag_list):
listener = StdOutListener(fetched_tweets_filename)
auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)
auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)
stream = Stream(auth, listener)
stream.filter(track=hash_tag_list)
# # # # TWITTER STREAM LISTENER # # # #
class StdOutListener(StreamListener):
def __init__(self, fetched_tweets_filename):
self.fetched_tweets_filename = fetched_tweets_filename
def on_data(self, data):
try:
print(data)
with open(self.fetched_tweets_filename, 'a') as tf:
tf.write(data)
return True
except BaseException as e:
print("Error on_data %s" % str(e))
return True
def on_error(self, status):
if status == 420:
return False
print(status)
if __name__ == '__main__':
hash_tag_list = ["omg"]# here is the filter to use
fetched_tweets_filename = "tweets.json"
twitter_streamer = TwitterStreamer()
twitter_streamer.stream_tweets(fetched_tweets_filename, hash_tag_list)
大概100%的我都不理解这种类型的代码,因为它可以完美地工作,并且可以将我需要的所有信息反馈给我,但是只提供与运行当天有关的数据。相反,我还需要前2/3周的数据。 有人可以帮我解决这个小问题吗? 非常感谢