簡體   English   中英

Twython-為什么會出現關鍵錯誤?

[英]Twython - why do I get a key error?

為什么會出現關鍵錯誤? 如何打印screen_name?

from twython import Twython

CONSUMER_KEY = "123"
CONSUMER_SECRET = "123"
OAUTH_TOKEN = "123"
OAUTH_TOKEN_SECRET = "123"
twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN,OAUTH_TOKEN_SECRET)

search_results = twitter.search(q='funny', count=1)

print(search_results["screen_name"])

search_results是頂級收集結果; 它包含兩個鍵: search_metadatastatuses列表。 后者是一個序列; 即使count=1您仍然需要對該結果建立索引

每條推文遵循一個固定的結構 ; 如果要獲取發布該推文的用戶的屏幕名稱,請查看'user'鍵,它具有'screen_name'鍵:

print(search_results['statuses'][0]['user']['screen_name'])

它總是有助於在交互式Python會話中查看結果:

>>> search_results = twitter.search(q='funny', count=1)
>>> type(search_results)
<class 'dict'>
>>> search_results.keys()
dict_keys(['search_metadata', 'statuses'])
>>> type(search_results['search_metadata'])
<class 'dict'>
>>> type(search_results['statuses'])
<class 'list'>
>>> from pprint import pprint
>>> pprint(search_results['search_metadata'])
{'completed_in': 0.015,
 'count': 1,
 'max_id': 564091573458051073,
 'max_id_str': '564091573458051073',
 'next_results': '?max_id=564091573458051072&q=funny&count=1&include_entities=1',
 'query': 'funny',
 'refresh_url': '?since_id=564091573458051073&q=funny&include_entities=1',
 'since_id': 0,
 'since_id_str': '0'}
>>> len(search_results['statuses'])
1
>>> type(search_results['statuses'][0])
<class 'dict'>
>>> search_results['statuses'][0].keys()
dict_keys(['in_reply_to_user_id_str', 'lang', 'user', 'metadata', 'created_at', 'retweeted', 'entities', 'in_reply_to_screen_name', 'place', 'id', 'retweet_count', 'geo', 'favorite_count', 'in_reply_to_status_id', 'contributors', 'in_reply_to_status_id_str', 'source', 'in_reply_to_user_id', 'id_str', 'favorited', 'text', 'retweeted_status', 'coordinates', 'truncated'])
>>> type(search_results['statuses'][0]['user'])
<class 'dict'>
>>> pprint(search_results['statuses'][0]['user'])
{'contributors_enabled': False,
 'created_at': 'Sat Sep 08 12:09:02 +0000 2012',
 'default_profile': True,
 'default_profile_image': False,
 'description': 'Nothing lasts forever.',
 'entities': {'description': {'urls': []}},
 'favourites_count': 947,
 'follow_request_sent': False,
 'followers_count': 211,
 'following': False,
 'friends_count': 242,
 'geo_enabled': False,
 'id': 810798062,
 'id_str': '810798062',
 'is_translation_enabled': False,
 'is_translator': False,
 'lang': 'fr',
 'listed_count': 0,
 'location': 'Lebanon',
 'name': 'Jøya',
 'notifications': False,
 'profile_background_color': 'C0DEED',
 'profile_background_image_url': 'http://abs.twimg.com/images/themes/theme1/bg.png',
 'profile_background_image_url_https': 'https://abs.twimg.com/images/themes/theme1/bg.png',
 'profile_background_tile': False,
 'profile_banner_url': 'https://pbs.twimg.com/profile_banners/810798062/1422692220',
 'profile_image_url': 'http://pbs.twimg.com/profile_images/557546794486226944/1us5bfgV_normal.jpeg',
 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/557546794486226944/1us5bfgV_normal.jpeg',
 'profile_link_color': '0084B4',
 'profile_location': None,
 'profile_sidebar_border_color': 'C0DEED',
 'profile_sidebar_fill_color': 'DDEEF6',
 'profile_text_color': '333333',
 'profile_use_background_image': True,
 'protected': False,
 'screen_name': 'JoyaFaddoul',
 'statuses_count': 7690,
 'time_zone': None,
 'url': None,
 'utc_offset': None,
 'verified': False}
>>> search_results['statuses'][0]['user']['screen_name']
'JoyaFaddoul'

修改print語句,如下所示:

print(search_results["user"]["screen_name"])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM