使用 topsy加载 JSON 时出错



当我加载单个记录时,当我尝试加载多个记录时,json创建得很好,我收到此错误。对不起,我是python http://tny.cz/ce1baaba 的新手

Traceback (most recent call last):
  File "TweetGraber.py", line 26, in <module>
    get_tweets_by_query(topic)
  File "TweetGraber.py", line 15, in get_tweets_by_query
    json_tree = json.loads(source)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 368, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 11 column 1 (char 2380 - 46974)

这是我的代码

def get_tweets_by_query(query, count=10):
    """A function that gets the tweets by a query."""
    Tweets=[]
    queryEncoded=urllib.quote(query)
    api_key = "xxxxx"
    source=urllib.urlopen("http://api.topsy.com/v2/content/bulktweets.json?q=%s&type=tweet&offset=0&perpage=%s&window=realtime&apikey=%s" % (queryEncoded, count, api_key)).read()
    json_tree = json.loads(source)
    pprint(json_tree)
topic = raw_input("Please enter a topic: ")
get_tweets_by_query(topic)

谢谢Timusan,我能够纠正我的json原始问题缺少根元素"[",这表明我们期待数组,并且每个对象结束后都缺少","。所以这里是固定代码。所以这是代码

def get_tweets_by_query(query, count=10):
    """A function that gets the tweets by a query."""
    Tweets=[]
    queryEncoded=urllib.quote(query)
    api_key = "xx"
    source=urllib.urlopen("http://api.topsy.com/v2/content/bulktweets.json?q=%s&type=tweet&offset=0&perpage=%s&window=realtime&apikey=%s" % (queryEncoded, count, api_key)).read()
    source="["+source+"]"
    source=source.replace("}n{","},{")
    json_tree = json.loads(source)
    pprint(json_tree)

相关内容

  • 没有找到相关文章

最新更新