【问题标题】:Twitter api python - sys.argv and jsonTwitter api python - sys.argv 和 json
【发布时间】:2012-02-10 18:22:03
【问题描述】:

我在http://pastebin.com/bqj3bZhG找到了这个

"""
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 文件,你能帮我吗

感谢您的帮助!!!!

【问题讨论】:

    标签: python json twitter


    【解决方案1】:

    我认为您对sys.argv[1] 的含义感到困惑。 pastebin 链接中提到

    输入参数是来自 Twitter API 的 JSON ascii 文件的文件名

    文件名 = sys.argv[1]

    因此,首先您必须使用 twitter api 下载您的 json ascii 文件,然后您必须在调用脚本时将其作为参数传递:

    python myscript.py jsonfile

    所以这里 jsonfile == sys.argv[1]

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多