【问题标题】:Tweepy StreamListener to CSVTweepy StreamListener 到 CSV
【发布时间】:2016-11-11 20:58:15
【问题描述】:

我是 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])

有什么建议吗?

【问题讨论】:

  • 您的第二个on_status 函数不在CustomStreamListener 类中。

标签: python python-3.x csv tweepy


【解决方案1】:

您试图用来编写 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'])

【讨论】:

  • 你是对的我错了,但现在看来字符有问题。当我尝试运行代码时,仅打印一些推文数据,然后返回此代码 'UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f44d' in position 111: character maps to ' 当遇到一些推文时会发生这种情况哪里有一些特殊字符我该如何解决这个问题。更何况我可以停止搜索推文,例如。在 1000 条推文之后?
  • 这可能是因为它无法打印推文文本的 unicode 部分。尝试使用status.text.encode('utf-8') 而不仅仅是status.text(在打印行和writerow 行中)。此外,使用 utf-8 编码打开文件:with open('OutputStreaming.txt', 'w'', encoding="utf8") as f:。对于其他问题(限制为 1000 条推文),请发布另一个问题。
  • 另外,如果回答对您有帮助,请采纳,以便以后遇到您问题的用户能够轻松找到解决方案。
猜你喜欢
  • 1970-01-01
  • 2022-10-16
  • 2018-01-06
  • 2018-08-13
  • 2015-07-08
  • 2017-09-04
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多