2012-01-15 55 views
1

我發現這對http://pastebin.com/bqj3bZhGTwitter的API的Python - 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]混淆。它在pastebin鏈接提到

輸入參數是JSON ASCII文件從Twitter的API文件名

名= sys.argv中[1]

所以,首先你必須使用twitter api下載您的json ascii文件,並且您必須將其作爲參數傳遞,同時調用您的腳本,如下所示:

python myscript.py jsonfil È

所以這裏jsonfile == sys.argv中[1]

和myscript.py == sys.argv中[0]