我正在尝试运行此脚本来收集特定主题标签的实时流式传输,并将其保存在csv文件中。这是我目前的脚本。它打印到控制台并创建一个包含作者,日期,文本行的csv文件。然而,流式推文不会保存到csv。
import sys
import tweepy
import csv
#pass security information to variables
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
#use variables to access twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
#create an object called 'customStreamListener'
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
print status.author.screen_name, status.created_at, status.text
with open('file.csv', 'w') as f:
f.write('Author,Date,Text')
writer = csv.writer(f)
writer.writerow([status.author.screen_name, status.created_at, status.text])
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
streamingAPI = tweepy.streaming.Stream(auth, CustomStreamListener())
streamingAPI.filter(track=['russia'])
答案 0 :(得分:0)
您需要在on_status之外使用全局变量编写器。
例如(使用您的代码)
class CustomStreamListener(tweepy.StreamListener):
global writer
writer = csv.writer(open('file.csv', 'w+'))
w.writerow(('Author', 'Date', 'Text'))
def on_status(self, status):
print status.author.screen_name, status.created_at, status.text
writer.writerow(status.author.screen_name, status.created_at, status.text)