2015-02-07 67 views
0

爲什麼我得到一個關鍵錯誤? 如何打印screen_name?Twython - 爲什麼我會得到一個關鍵錯誤?

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"]) 

回答

1

search_results是一個頂層收集結果;它包含兩個鍵:search_metadata和一個statuses列表。後者是一個序列;即使與count=1你仍然需要索引結果。

每條推文follows a set structure;如果你想拿到張貼鳴叫用戶的網名,看'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' 
+0

我仍然得到同樣的錯誤。 KeyError:'screen_name' – furz 2015-02-07 16:19:44

+0

@furz:那麼在'statuses'中返回什麼呢?第一種狀態是否有「用戶」鍵? – 2015-02-07 16:22:33

0

修改print聲明如下:

print(search_results["user"]["screen_name"]) 
相關問題