2012-01-15 5 views
1

私はhttp://pastebin.com/bqj3bZhGTwitterのAPIパイソン - sys.argvのとJSON

""" 
Simple Python example showing how to parse JSON-formatted Twitter messages+metadata 
(i.e. data produced by the Twitter status tracking API) 

This script simply creates Python lists containing the messages, locations and timezones 
of all tweets in a single JSON file. 

Author: Geert Barentsen - 4 April (#dotastro) 
""" 

import sys 
import simplejson 
import difflib 

# Input argument is the filename of the JSON ascii file from the Twitter API 
filename = sys.argv[1] 

tweets_text = [] # We will store the text of every tweet in this list 
tweets_location = [] # Location of every tweet (free text field - not always accurate or  given) 
tweets_timezone = [] # Timezone name of every tweet 

# Loop over all lines 
f = file(filename, "r") 
lines = f.readlines() 
for line in lines: 
    try: 
      tweet = simplejson.loads(line) 

      # Ignore retweets! 
      if tweet.has_key("retweeted_status") or not tweet.has_key("text"): 
        continue 

      # Fetch text from tweet 
      text = tweet["text"].lower() 

      # Ignore 'manual' retweets, i.e. messages starting with RT    
      if text.find("rt ") > -1: 
        continue 

      tweets_text.append(text) 
      tweets_location.append(tweet['user']['location']) 
      tweets_timezone.append(tweet['user']['time_zone']) 

    except ValueError: 
      pass 


# Show result 
print tweets_text 
print tweets_location 
print tweets_timezone 

まあ上でこれを見つけたが、私は私の知る限り、それは私がにJSONファイルをインポートする必要があります理解して...

を、それを使用傾けます
ファイル名= sys.argvの[1]

しかし

import urllib 
#twitteruser 
user="gigmich" 

#open twitter timeline request 
filename = sys.argv[urllib.urlopen("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&contributor_details&include_rts=true&screen_name="+user+"&count=3600")] 

は、あなたが私を助けてください可能性が私

のために動作するようには思えない私は、JSONファイルにあなたの助けを

感謝を挿入する必要がありました!!!!

答えて

2

sys.argv[1]の意味で混乱していると思います。それは最初に、そう

入力引数TwitterのAPIからJSON ASCIIファイルのファイル名です

ファイル名= sys.argvの[1]

ことpastebinリンクに記載されていますtwitter apiを使ってjson asciiファイルをダウンロードしなければならず、あなたのスクリプトをこのように呼び出す際に引数として渡す必要があります:

python myscript.py jsonfil E

ので、ここjsonfile == sys.argvの[1]

とmyscript.py == sys.argvの[0]