2012-04-05 4 views

答えて

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 yourpathとファイル名 "

tweetstream argparse gzip jsonとastモジュールがインストールされている必要があります。これらはすべてpipやeasy_install、あるいはほとんどのubuntu/fedoraパッケージマネージャーからインストールできます。

スクリプトが作成する出力ファイルは、単純なgzip圧縮テキストファイルです。各行は、完全なツイートjsonオブジェクトを含む新しいjson文字列です。レート制限に達するまでスクリプトが実行されるため、適切なEOFでgzipファイルを閉じることはありません。しかし、Pythonは気にしないので、別のスクリプトで開くことも、7zipやwinrarで開くこともできます。

私は役立つことを願っています。 :)

関連する問題