试图解析来自twitter API的数据

时间:2015-11-18 22:12:49

标签: python json twitter

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

我试图在python中使用JSON.loads()加载每个推文,但我一直在遇到错误。 JSON valueError反馈不够具体,不足以指出我的错误,我一直在努力寻找错误。

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

我真的很感激有关如何做的任何建议!

2 个答案:

答案 0 :(得分:2)

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

"contributors": None
  • 应将None更改为null(不带引号)。
  • 字符串不应以“u”为前缀。
  • 真与假应该是真的和假的(没有引号)。

参见维基百科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"

答案 1 :(得分:1)

您的JSON无效。

的问题:

  • None应该成为null
  • True应该成为true
  • False应该成为false
  • 网址中不能包含双引号。将它们更改为单引号或转义。
    • "source": "<a href="http://www.snsanalytics.com" rel="nofollow">SNS Analytics</a>"应该成为"source": "<a href='http://www.snsanalytics.com' rel='nofollow'>SNS Analytics</a>"
  • 你有很长的句子以L - 660902084456288256L结尾。移除L并将其设为660902084456288256
  • 另外,当你解析它时,请确保在任何字符串前面没有任何字符,但这可能只是因为它打印出unicode,所以请确保。

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

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

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

相关问题