python - 使用 Python 解析用户个人资料数据的 Twitter API JSON 输出时出现问题

标签 python json parsing twitter

我认为另一个SO线程会回答我的问题(http://stackoverflow.com/questions/4883751/trouble-reading-json-object-in-python),因为它与我的问题非常相似,但是那里的数据与我的案例中的数据略有不同。

我从 Twitter API 中提取了大约 470 条记录来获取 Twitter 用户数据,例如:

{
steve: {
follow_request_sent: false,
profile_use_background_image: true,
default_profile_image: false,
geo_enabled: true,
verified: false,
profile_image_url_https: "https://si0.twimg.com/profile_images/1416115378/profile_normal.jpg",
profile_sidebar_fill_color: "F8E846",
id: 1376271,
profile_text_color: "000000",
followers_count: 2042,
profile_sidebar_border_color: "FFFFFF",
location: "Dallas and 51°33′28″N 0°6′10″W",
profile_background_color: "7d0000",
listed_count: 110,
status: {
favorited: false,
contributors: null,
truncated: false,
text: "So Microsoft's cloud is down. Can't say I have noticed. To the cloud! (the Amazon one of course)",
created_at: "Wed Feb 29 15:51:44 +0000 2012",
retweeted: false,
in_reply_to_status_id: null,
coordinates: null,
id: 174884564718723070,
source: "<a href="http://www.tweetdeck.com" rel="nofollow">TweetDeck</a>",
in_reply_to_status_id_str: null,
in_reply_to_screen_name: null,
id_str: "174884564718723073",
place: null,
retweet_count: 0,
geo: null,
in_reply_to_user_id_str: null,
in_reply_to_user_id: null
},
utc_offset: -21600,
statuses_count: 11504,
description: "Network engineer. Cisco, Juniper, F5, HP, EMC, etc. If it is in the data center I deal with it. Arsenal and Mavericks supporter to the max over at @steverossen",
friends_count: 822,
profile_link_color: "0000ff",
profile_image_url: "http://a0.twimg.com/profile_images/1416115378/profile_normal.jpg",
is_translator: false,
show_all_inline_media: false,
profile_background_image_url_https: "https://si0.twimg.com/profile_background_images/192104695/stadium.jpg",
id_str: "1376271",
profile_background_image_url: "http://a2.twimg.com/profile_background_images/192104695/stadium.jpg",
screen_name: "steve",
lang: "en",
profile_background_tile: false,
favourites_count: 0,
name: "Steve Rossen",
notifications: false,
url: "http://steverossen.com",
created_at: "Sat Mar 17 21:36:32 +0000 2007",
contributors_enabled: false,
time_zone: "Central Time (US & Canada)",
protected: false,
default_profile: false,
following: false
},
}

问题是每条记录都以该人的 Twitter 句柄开头,因此每条记录都是不同的。所以我只能使用:

import json
import csv

f = open('my.json')
data = json.load(f)
f.close()

for item in data:
    print item

打印出这些句柄,但不知道如何在没有 key 的情况下进入每个人的记录。

我在这里严重忽略了什么?我至少想获取“描述”,它作为键嵌套在用户名中。

最佳答案

也许我缺少您正在寻找的东西,但您不能这样做:

import json

f = open('my.json')  
data = json.load(f)
f.close()

for key in data.keys():
    print data[key]["description"]

关于python - 使用 Python 解析用户个人资料数据的 Twitter API JSON 输出时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9505593/

相关文章:

python - 如何处理 API 响应

python - 在 dataFrame 的索引上应用匿名函数

javascript - 将 JSON 转换为 topoJSON 时出现意外的线/多边形

python - 无法使用 json.loads 将字符串加载到对象中

java - 使用 UTF-8 字符将 ObjectNode 写入 JSON 字符串以转义 ASCII

python - 使用 Python 解析一个简单的文本文件

python - 将两个正则表达式连接在一起

java - 确保至少一个消费者收到在主题交换上发布的消息

Python - PyGame - 学习向量/动画

c - 解析混合类型数据的最佳实践?