【问题标题】:Trying to parse data from twitter API尝试从 twitter API 解析数据
【发布时间】:2016-02-20 20:41:04
【问题描述】:

我在将 twitter 数据读入 python 时遇到了很多麻烦。我在以下输出 http://pastebin.com/b4ZAUPsY 中有推文。

我尝试在 python 中使用 JSON.loads() 加载每条推文,但我一直遇到错误。 JSON valueError 反馈不够具体,无法指出问题所在,我一直在努力通过肉眼找到错误。

我还尝试了 ast.literal_eval() 希望我可以直接将数据加载为字典,但我也无法让这个想法发挥作用。

我非常感谢任何关于如何做的建议!

【问题讨论】:

    标签: python json twitter


    【解决方案1】:

    这不是有效的 JSON。您遇到的问题之一与 None 的值有关。

    "contributors": None
    
    • None 应更改为 null(不带引号)。
    • 字符串不应以'u'为前缀。
    • True 和 False 应该是 true 和 false(不带引号)。

    参见维基百科https://en.m.wikipedia.org/wiki/JSON

    你的数据几乎是有效的python,可以用下面的代码解析:

    import re
    
    a = '{u"contributors": None, u"truncated": False, u"text": u"Uber Germany retreats to Berlin, Munich https://t.co/OUTjo2vMgb", u"is_quote_status": False, u"in_reply_to_status_id": None, u"id": 660902084456288256L, u"favorite_count": 0, u"source": u"<a href="http://www.snsanalytics.com" rel="nofollow">SNS Analytics</a>", u"retweeted": False, u"coordinates": None, u"timestamp_ms": u"1446406310558", u"entities": {u"user_mentions": [], u"symbols": [], u"hashtags": [], u"urls": [{u"url": u"https://t.co/OUTjo2vMgb", u"indices": [40, 63], u"expanded_url": u"http://www.snsanalytics.com/iV9Oy0", u"display_url": u"snsanalytics.com/iV9Oy0"}]}, u"in_reply_to_screen_name": None, u"id_str": u"660902084456288256", u"retweet_count": 0, u"in_reply_to_user_id": None, u"favorited": False, u"user": {u"follow_request_sent": None, u"profile_use_background_image": True, u"default_profile_image": False, u"id": 119396644, u"verified": False, u"profile_image_url_https": u"https://pbs.twimg.com/profile_images/1225936492/Munich_normal.jpg", u"profile_sidebar_fill_color": u"DDEEF6", u"profile_text_color": u"333333", u"followers_count": 3701, u"profile_sidebar_border_color": u"C0DEED", u"id_str": u"119396644", u"profile_background_color": u"C0DEED", u"listed_count": 59, u"profile_background_image_url_https": u"https://pbs.twimg.com/profile_background_images/197414716/munich_places.jpg", u"utc_offset": 3600, u"statuses_count": 29594, u"description": None, u"friends_count": 397, u"location": u"Munich, Germany", u"profile_link_color": u"0084B4", u"profile_image_url": u"http://pbs.twimg.com/profile_images/1225936492/Munich_normal.jpg", u"following": None, u"geo_enabled": False, u"profile_background_image_url": u"http://pbs.twimg.com/profile_background_images/197414716/munich_places.jpg", u"name": u"Munich Daily", u"lang": u"en", u"profile_background_tile": True, u"favourites_count": 0, u"screen_name": u"MunichDaily", u"notifications": None, u"url": None, u"created_at": u"Wed Mar 03 14:31:12 +0000 2010", u"contributors_enabled": False, u"time_zone": u"Amsterdam", u"protected": False, u"default_profile": False, u"is_translator": False}, u"geo": None, u"in_reply_to_user_id_str": None, u"possibly_sensitive": False, u"lang": u"en", u"created_at": u"Sun Nov 01 19:31:50 +0000 2015", u"filter_level": u"low", u"in_reply_to_status_id_str": None, u"place": None}'
    a = re.sub(', u"source": u"<a href=', ', u"source": ', a)
    a = re.sub(' rel="nofollow">SNS Analytics</a>",', ',', a)
    a = eval(a)
    

    之所以不完全是python语法是因为这部分:-

    u"source": u"<a href="http://www.snsanalytics.com" rel="nofollow">SNS Analytics</a>"
    

    包含在此字符串中的 html 超链接标记还包含未转义的引号。

    上面的代码将其转换为:-

    u"source": u"http://www.snsanalytics.com"
    

    【讨论】:

    • 感谢您的帮助!
    【解决方案2】:

    您的 JSON 无效。

    问题:

    • None 应该变成 null
    • True 应该变成 true
    • False 应该变成 false
    • URL 中不能有双引号。将它们更改为单引号或转义。
      • "source": "&lt;a href="http://www.snsanalytics.com" rel="nofollow"&gt;SNS Analytics&lt;/a&gt;" 应该变成 "source": "&lt;a href='http://www.snsanalytics.com' rel='nofollow'&gt;SNS Analytics&lt;/a&gt;"
    • 你有一个以L - 660902084456288256L结尾的long。删除 L 并将其改为 660902084456288256
    • 另外,当您解析它时,请确保任何字符串前面都没有 u,但这可能只是因为它打印出 unicode 的方式,所以请确保。

    这是有效的 JSON:http://pastebin.com/tqGscNhA

    将来,您可以使用 JSONLint 来验证您的数据:http://jsonlint.com/

    查看http://json.org/。右侧有一个白色矩形焦点块,指定正确的语法和所有有效类型。

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 2017-01-22
    • 1970-01-01
    • 2017-07-13
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多