Tweepy StreamListener转换为CSV [英] Tweepy StreamListener to CSV
问题描述
我是python的新手,我正在尝试开发一个应用程序,该应用程序使用Tweepy和Streaming API从Twitter检索数据,并将数据转换为CSV文件. 问题在于此代码不会创建输出CSV文件,可能是因为我应该将代码设置为在达到例如时停止. 1000条推文,但我无法设置此停止点
I'm a newbie on python and I'm trying to develop an app that retrieves data from Twitter with Tweepy and the Streaming APIs and converts the data on a CSV file. The problem is that this code doesn't create an output CSV file, maybe because I should set the code to stop when it achieves for eg. 1000 tweets but I'm not able to set this stop point
这是代码
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)
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=['Dallas', 'NewYork'])
def on_status(self, status):
with open('OutputStreaming.txt', 'w') as f:
f.write('Author,Date,Text')
writer = csv.writer(f)
writer.writerow([status.author.screen_name, status.created_at, status.text])
有什么建议吗?
推荐答案
您尝试编写csv的函数永远不会被调用.
我假设您想在CustomStreamListener.on_status
中编写此代码.
另外,您必须将标题一次写入文件(流侦听器外部).
看一下这段代码:
The function that you are trying to write the csv with is never called.
I assume you wanted to write this code in CustomStreamListener.on_status
.
Also, you have to write the titles to the file once (outside the stream listener).
Take a look at this code:
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)
# Writing status data
with open('OutputStreaming.txt', 'a') as f:
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
# Writing csv titles
with open('OutputStreaming.txt', 'w') as f:
writer = csv.writer(f)
writer.writerow(['Author', 'Date', 'Text'])
streamingAPI = tweepy.streaming.Stream(auth, CustomStreamListener())
streamingAPI.filter(track=['Dallas', 'NewYork'])
这篇关于Tweepy StreamListener转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!