Tweepy StreamListener 到 CSV
Tweepy StreamListener to CSV
我是 python 的新手,我正在尝试开发一个应用程序,使用 Tweepy 和 Streaming API 从 Twitter 检索数据并将数据转换为 CSV 文件。
问题是这段代码不会创建输出 CSV 文件,可能是因为我应该将代码设置为在实现时停止,例如。 1000 条推文,但我无法设置此停止点
这是代码
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
中编写此代码。
此外,您必须将标题写入文件一次(在流侦听器之外)。
看看这段代码:
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'])
我是 python 的新手,我正在尝试开发一个应用程序,使用 Tweepy 和 Streaming API 从 Twitter 检索数据并将数据转换为 CSV 文件。 问题是这段代码不会创建输出 CSV 文件,可能是因为我应该将代码设置为在实现时停止,例如。 1000 条推文,但我无法设置此停止点
这是代码
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
中编写此代码。
此外,您必须将标题写入文件一次(在流侦听器之外)。
看看这段代码:
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'])