【问题标题】:storing a json file from twitter's api in mongoDB在 mongoDB 中存储来自 twitter 的 api 的 json 文件
【发布时间】:2012-04-05 09:50:25
【问题描述】:

我正在使用带有 twitter 搜索 api 的 java 在 mongoDB 中存储推文,有没有办法按原样存储来自 twitter api 的 json?

我也在考虑用python,有没有办法用python做呢?

【问题讨论】:

    标签: java python json twitter bson


    【解决方案1】:

    我不知道你是否仍然对这个问题的答案感兴趣,但从纯粹的 python 立场来看,这就是我存储原始推文 json 的方式:

    import tweetstream # Needed For Twitter API Capture (Make sure using modified version with proxy support)
    import argparse    # Needed for taking cmd line input
    import gzip        # Needed for compressing output
    import json        # Needed for Data conversion for easier DB import
    import ast         # Also Needed for Data conversion
    
    collector = argparse.ArgumentParser(description='Collect a lot of Tweets')        # This line sets up the argument collector
    collector.add_argument('--username', dest='username', action="store")             # This line collects the Username
    collector.add_argument('--password', dest='password', action="store")             # This line collects the password
    collector.add_argument('--outputfilename', dest='outputfilename', action="store") # This line collects the output filename
    
    args = collector.parse_args()                                                     # Setup args to store cmd line arguments
    
    def printusername():                                                              # define the username argument
    
            print args.username
    
    def printpassword():                                                              # define the password argument
    
            print args.password
    
    def printoutputfilename():                                                        # define the output filename
    
            print args.outputfilename
    
    output=gzip.open(args.outputfilename, "a")                                        # Open the output file for GZIP writing
    
    with tweetstream.TweetStream(args.username, args.password) as stream:             # Open the Twitter Stream
        for tweet in stream:                                                          # For each tweet within the twitter stream
            line = str(tweet)                                                         # turn the tweet into a string
            line = ast.literal_eval(line)                                             # evaluate the python string (dictionary)
            line = json.dumps(line)                                                   # turn the python dictionary into valid JSON
            output.write(line)                                                        # write the line to the output file
            output.write("\n")  
    

    只运行这个:"python myscript.py --username yourusername --password yourpassword --outputfilename yourpathandfilename"

    您需要安装 tweetstream argparse gzip json 和 ast 模块。所有这些都可以通过 pip 或 easy_install 或大多数 ubuntu/fedora 包管理器安装。

    脚本将创建的输出文件是一个简单的 gzip 压缩文本文件,其中每一行都是一个新的 json 字符串,其中包含完整的 tweet json 对象。由于脚本一直运行到您达到速率限制,所以它不会使用正确的 EOF 关闭 gzip 文件。但是python不在乎,所以你可以用另一个脚本打开它,7zip或winrar也不在乎。

    我希望这会有所帮助。 :)

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 2011-05-11
      • 2021-12-21
      • 2023-03-18
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      相关资源
      最近更新 更多