2012-04-05 1 views

답변

1

이 내가 원시 트윗 JSON 저장하는 방법입니다 서 여기에 있지만 순수하게 파이썬에서 여전히 대답에 관심이 있다면 나도 몰라 "파이썬로 myscripf :

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") 

이 단지를 실행하기를 .py --username yourusername --password yourpassword --outputfilename yourpathandfilename "

트위터 스트림 argparse gzip json 및 ast 모듈이 설치되어 있어야합니다. 이 모두는 pip 또는 easy_install 또는 대부분의 우분투/fedora 패키지 관리자를 통해 설치할 수 있습니다.

스크립트가 생성 할 출력 파일은 간단한 gzip 압축 텍스트 파일로 각 줄은 완전한 json 문자열을 포함하는 새로운 json 문자열입니다. 속도 제한값에 도달 할 때까지 스크립트가 실행되기 때문에 적절한 EOF로 gzip 파일을 닫지 않습니다. 그러나 파이썬은 상관 없으므로 다른 스크립트로 열거 나 7zip이나 winrar로 열 수는 없습니다.

도움이 되었기를 바랍니다. :)