为什么会出现密钥错误? 如何打印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"])
答案 0 :(得分: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'
答案 1 :(得分:0)
修改'X', 'Y', 'Z'
语句,如下所示:
print