【问题标题】:Output Tweepy TwitterStreamer to .csv from Python 2.7从 Python 2.7 输出 Tweepy TwitterStreamer 到 .csv
【发布时间】:2013-07-23 17:23:17
【问题描述】:

我有一个用 Python 编写的文件,它发送到 Twitterstream 并根据列表中的关键字抓取消息。列表很长,输出不是我想要的。我想清理文件并将结果输出到文本文件。

这是我当前的代码,它将所有消息写入一行:

import sys
....

if __name__ == '__main__':
     with open("keywords.txt", "r") as f:
         keywords = f.readlines()


    l = StdOutListener()    
    auth = OAuthHandler(consumer_key, consumer_secret)    
    auth.set_access_token(access_token, access_token_secret)    

    stream = Stream(auth, l)        
    stream.filter(track=keywords]) 

以上内容没有拉入任何内容,这意味着当我在命令提示符python hashtagworking.py > output.txt 下键入以下内容时,没有任何内容输出到文本文件中 stream.filter 中有大约 300 个项目,所以我想使用 txt 文件代替实际文本。此外,每条消息的结果都显示在一行上,这是它的编写方式,我想将其重写为消息中每个对象的输出到 csv 文件。

我想这就是我正在寻找的,但想确认一下:similar problem

我还想从其他嵌套对象(例如实体:{...})中获取内容,特别是我想从实体对象中获取主题标签,更具体地说是任何对象。我试过data.text.hashtagdata.entities.hashtagdata.entities.media.hashtag之类的东西都无济于事。

【问题讨论】:

    标签: python csv python-2.7 twitter export-to-csv


    【解决方案1】:

    对于您的关键字问题,假设您已将它们全部放在一个 txt 文件中(每行一个标记)

    with open("tokens.txt", "r") as f:
        tokens = f.readlines()
    
    ....
    stream.filter(track=tokens)
    

    对于您的其他问题(以 .csv 格式输出),您能否在文件中写下您想要的示例?

    class StdOutListener(StreamListener):        
            """ A listener handles tweets are the received from the stream.
            This is a basic listener that just prints received tweets to stdout.
    
            """        
        def on_status(self, data):            
            try:                
                print '%s , %s , %s , %s' % (data.text,\ <-- change to data.csv?               
                data.author.screen_name,data.created_at,data.source)
                with open("data.csv", 'a+') as f:
                    f.write("{text},{name},{created},{source}\n"
                            .format(text=str(data.text), 
                                    name=str(data.author.screen_name), 
                                    created=str(data.created_at), 
                                    source=str(data.source)))               
                return True            
            except Exception, e:                
                print >> sys.stderr, 'Encountered Exception:', e                
                pass        
    
        def on_error(self, status):            
            return True
    

    请注意,这不是一个可接受的长期解决方案,因为每次从流中过滤推文(也称为泛洪 I/O)时,您都在打开和关闭文件,您可以做的是实现一个缓冲区(每次缓冲区被填满时,将其转储到文件中。

    请注意,我是手动写入 csv 文件,如果您想更深入地了解 csv 操作,请查看http://docs.python.org/2/library/csv.html

    【讨论】:

    • 谢谢@ketouem 对于 csv 文件,如果我一天有 5 个元素,然后在第二天添加第 6 个元素,我希望转储任何在代码中分隔的内容要添加的第 6 个元素。我将查看您列出的文档,我还必须弄清楚如何从entities 部分中分解元素,我可能必须将其定义为一个函数,或者只是学习如何分解 json。
    • 我将代码更新为我现在正在处理的问题
    • 请注意,您可以使用同名(&内置)模块docs.python.org/2/library/json.html轻松操作原始 json 数据
    猜你喜欢
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 2020-09-19
    相关资源
    最近更新 更多