1

我正在使用 twitter API 来获取推文。这是我的代码。我收到一个错误(“无法解码任何 JSON 对象”)。

import json
import urllib
def main():
    f1 = open('tweet-stream.json')
    count = 0
    for line in f1:
        d = json.loads(line)
        lang =  d['user']['lang']
        status_count = d['user']['statuses_count']
        print 'Language: ' + lang + 'Status Count' + str(status_count)

if __name__ == "__main__":
    main()

这是我在终点站

Traceback (most recent call last):
  File "parseJSON.py", line 14, in <module>
    main()
  File "parseJSON.py", line 8, in main
    d = json.loads(line)
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 360, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 378, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

这是存储在 JSON 文件中的推文之一

{"created_at":"Sat Mar 16 06:12:21 +0000 2013","id":312808537338310656,"id_str":"312808537338310656","text":"\u30d6\u30ed\u30b0\u3092\u66f4\u65b0\u3057\u307e\u3057\u305f\u3002 \u300e\u30102013\u5e74\u3000Toshu\u30fbDIARY \u958b\u904b\u30c7\u30b6\u30a4\u30f3\uff06\u30ab\u30e9\u30fc\u6df1\u898b\u6771\u5dde\u6240\u9577\u76e3\u4fee\u3011\u300f http:\/\/t.co\/NiOLa0pc8C","source":"\u003ca href=\"http:\/\/www.ameba.jp\/\" rel=\"nofollow\"\u003eAmeba_Blog\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":559152618,"id_str":"559152618","name":"miloku","screen_name":"UsUsafukami","location":"Japan","url":"http:\/\/www.worldmate.or.jp","description":"\u3010\u5de8\u5927\u5730\u9707\u30fb\u5bcc\u58eb\u5c71\u306e\u7206\u767a\u30fb\u81ea\u7136\u707d\u5bb3\u304c\u8d77\u304d\u307e\u305b\u3093\u3088\u3046\u306b\u3011","protected":false,"followers_count":1109,"friends_count":1993,"listed_count":1,"created_at":"Sat Apr 21 00:54:26 +0000 2012","favourites_count":3,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":18457,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"642D8B","profile_background_image_url":"http:\/\/a0.twimg.com\/images\/themes\/theme10\/bg.gif","profile_background_image_url_https":"https:\/\/si0.twimg.com\/images\/themes\/theme10\/bg.gif","profile_background_tile":true,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3370777841\/6415f2a1d738896eddf1e5f3d9a308a8_normal.png","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3370777841\/6415f2a1d738896eddf1e5f3d9a308a8_normal.png","profile_link_color":"FF0000","profile_sidebar_border_color":"65B0DA","profile_sidebar_fill_color":"7AC3EE","profile_text_color":"3D1957","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"entities":{"hashtags":[],"urls":[{"url":"http:\/\/t.co\/NiOLa0pc8C","expanded_url":"http:\/\/amba.to\/XsoEt0","display_url":"amba.to\/XsoEt0","indices":[53,75]}],"user_mentions":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"medium"}
4

0 回答 0