推文只是 python 字典,它们的内容与Tweet
资源完全一致。因此,每条推文都有一个id_str
键:
print tweet['id_str']
如果事情不清楚,您总是可以打印数据结构;我可以推荐使嵌套 python 结构更具可读性的pprint.pprint()
函数:
import pprint
pprint.pprint(tweet)
示例会话:
>>> from twython import Twython
>>> t = Twython()
>>> res = t.search(q='python')
>>> res.keys()
[u'next_page', u'completed_in', u'max_id_str', u'since_id_str', u'refresh_url', u'results', u'since_id', u'results_per_page', u'query', u'max_id', u'page']
>>> from pprint import pprint
>>> pprint(res[u'results'][0])
{u'created_at': u'Mon, 17 Sep 2012 21:01:12 +0000',
u'from_user': u'Me_Craay_GOOFY',
u'from_user_id': 230100184,
u'from_user_id_str': u'230100184',
u'from_user_name': u'\u06deSuperFLY_PUER\u06de\u2122',
u'geo': None,
u'id': 247802407529115649,
u'id_str': u'247802407529115649',
u'iso_language_code': u'en',
u'metadata': {u'result_type': u'recent'},
u'profile_image_url': u'http://a0.twimg.com/profile_images/2617747450/345616051_normal.jpg',
u'profile_image_url_https': u'https://si0.twimg.com/profile_images/2617747450/345616051_normal.jpg',
u'source': u'<a href="http://globalgrind.com">UncleUber for Blackberry</a>',
u'text': u'RT @Mr_Oyato: #ViolentPrayers May the python of breakthrough swallow you and your household today.',
u'to_user': None,
u'to_user_id': 0,
u'to_user_id_str': u'0',
u'to_user_name': None}
>>> res[u'results'][0]['id_str']
u'247802407529115649'