【发布时间】:2014-03-26 15:50:16
【问题描述】:
对于使用 tweepy 和 Twitter 的 API 来说是全新的,我意识到(为时已晚)我在收集一些 Twitter 数据时犯了许多错误。我一直在收集有关冬季奥运会的推文,并一直在使用 Streaming API 按搜索词进行过滤。但是,我只检索了文本、日期时间和推文 ID,而不是检索所有可用数据。下面是一个实现的流监听器示例:
import os
import sys
import tweepy
os.chdir('/my/preferred/location/Twitter Olympics/Data')
consumer_key = 'cons_key'
consumer_secret = 'cons_sec'
access_token = 'access_token'
access_secret = 'access_sec'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
# count is used to give an approximation of how many tweets I'm pulling at a given time.
count = []
f = open('feb24.txt', 'a')
class StreamListener(tweepy.StreamListener):
def on_status(self, status):
print 'Running...'
info = status.text, status.created_at, status.id
f.write(str(info))
for i in info:
count.append(1)
def on_error(self, status_code):
print >> sys.stderr, "Encountered error with status code: ", status_code
def on_timeout(self):
print >> sys.stderr, "Timeout..."
return True
sapi = tweepy.streaming.Stream(auth, StreamListener())
sapi.filter(track=["olympics", "olympics 2014", "sochi", "Sochi2014", "sochi 2014", "2014Sochi", "winter olympics"])
.txt 文件中存储的输出示例如下:
('RT @Visa: There can only be one winner. Soak it in #TeamUSA, this is your #everywhere #Sochi2014 @987654321@', datetime.datetime(2014, 2, 15, 18, 9, 51), 111111111111111111).
所以,这是我的问题。如果我能够在列表中获取 Tweet ID,有没有办法遍历这些以查询 Twitter Rest API 并检索完整的 JSON 文件?我的预感是肯定的,但我不确定实现,主要是关于如何将结果数据保存为 JSON 文件(因为我在这里一直使用 .txt 文件)。提前感谢您的阅读。
【问题讨论】:
标签: python json rest twitter tweepy