【发布时间】:2020-10-10 14:54:32
【问题描述】:
我正在使用 twitter 流 API,但我无法访问 screen_name 属性,因为嵌套的各个层让我有点困惑
这是我尝试过的
results = t.search(q='tuberculosis', count=50)
all_tweets = results['statuses']
for tweet in all_tweets:
tweetString = tweet["text"]
name = tweet["entities"]["user_mentions"][0]["screen_name"]
time = tweet["created_at"]
df1['Tweet'] = [tweetString]
df1['Name'] = [name]
df1['Time'] = [time]
df = df.append(df1)
这是 JSON 布局:
[
{
"created_at": "Thu Apr 06 15:28:43 +0000 2017",
"id": 850007368138018817,
"id_str": "850007368138018817",
"text": "RT @TwitterDev: 1/ Today we’re sharing our vision for the future of the Twitter API platform!nalink",
"truncated": false,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [
{
"screen_name": "TwitterDev", <------ I want to access this
"name": "TwitterDev",
"id": 2244994945,
"id_str": "2244994945",
"indices": [
3,
14
]
}
],
"urls": [
{
"url": "Link",
"expanded_url": "https://cards.twitter.com/cards/18ce53wgo4h/3xo1c",
"display_url": "cards.twitter.com/cards/18ce53wg…",
"indices": [
94,
117
]
}
]
},
"source": "<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>",
"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": 6253282,
"id_str": "6253282",
"name": "Twitter API",
"screen_name": "twitterapi",
"location": "San Francisco, CA",
"description": "The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
"url": "Link",
"entities": {
"url": {
"urls": [
{
"url": "Link",
"expanded_url": "https://dev.twitter.com",
"display_url": "dev.twitter.com",
"indices": [
0,
22
]
}
]
},
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 6172353,
"friends_count": 46,......
【问题讨论】:
-
您是否遇到任何错误?或者当你使用 tweet["entities"]["user_mentions"][0]["screen_name"] 时会发生什么?
-
是的,我收到了错误
list index is out of range
标签: python json twitter twython