如何将 tweepy Twitter 流保存到文件? [英] How to save a tweepy Twitter stream to a file?
问题描述
我有一个工作脚本,可以成功收集提及stackoverflow"的推文.但是,我想在 iPython 中运行脚本(而不是执行单独的 .py 文件).理想情况下,我只想打开它 ipyb 文件,选择全部运行,然后让它运行一周左右(当然不是关闭我的笔记本电脑),结果我有一个 .json 文件,其中包含一周的推文.>
这是我目前所拥有的:
from tweepy import Stream从 tweepy 导入 OAuthHandler从 tweepy.streaming 导入 StreamListeneraccess_token = "x"access_token_secret = "x"消费者密钥 = "x"消费者秘密=x"# 要打开的文件名是第二个参数save_file = open('data.json', 'a')类侦听器(StreamListener):def on_data(self, data):打印(数据)返回真def on_error(self, status):打印(状态)auth = OAuthHandler(consumer_key,consumer_secret)auth.set_access_token(access_token, access_token_secret)twitterStream = Stream(auth, listener())twitterStream.filter(track=["stackoverflow"])
将以下代码添加到现有代码中.'fetched_tweets.txt'是以'a'(追加模式)打开的推文的保存文件名.
class StdOutListener(StreamListener):def on_data(self, data):#打印数据使用 open('fetched_tweets.txt','a') 作为 tf:tf.write(数据)返回真def on_error(self, status):打印状态
I have a working script that successfully gathers tweets that mention "stackoverflow". However, I want to run the script in iPython (rather than executive a separate .py file). Ideally, I just want to open it ipyb file, select run all, and let it run for a week or so (not closing my laptop of course) and in result I have a .json file with a week's worth of tweets.
Here is what I have so far:
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
access_token = "x"
access_token_secret = "x"
consumer_key = "x"
consumer_secret = "x"
# file name that you want to open is the second argument
save_file = open('data.json', 'a')
class listener(StreamListener):
def on_data(self, data):
print(data)
return True
def on_error(self, status):
print(status)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["stackoverflow"])
add the following code to your existing code. 'fetched_tweets.txt' is the name of file in which you want to save the tweets which is opened in 'a'(append mode).
class StdOutListener(StreamListener):
def on_data(self, data):
#print data
with open('fetched_tweets.txt','a') as tf:
tf.write(data)
return True
def on_error(self, status):
print status
这篇关于如何将 tweepy Twitter 流保存到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!