python - 尝试从 Twitter API 解析数据

标签 python json twitter

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

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

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

我真的很感激任何关于该怎么做的建议!

最佳答案

这不是有效的 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"

关于python - 尝试从 Twitter API 解析数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33791462/

相关文章:

python - 代码检测网络摄像头时,cv2 模块未捕获图像

python - IP地址正则表达式python

python - 不存储指针的 N​​eedleman-Wunsch 全局比对中的回溯

php - 如何用 json 回显数组?

javascript - 无法使用 ng-repeat 打印对象的值

ios - Twitter iOS API 是否提供任何方法来确定推文是否成功?

python - Tkinter 标签重叠

java - 来自 Api 调用的 Java 中的 Json 建模

java - 如何从twitter返回的json中提取文本和用户名?

android - 在哪里放置 Twitter Key for Android 的 API key