解析tweepy响应

时间:2017-07-12 02:23:40

标签: python data-structures tweepy

我正在使用tweepy搜索区块链的推文,因为我正在使用以下代码:

验证

consumerKey = ""
consumerSecret = ""
accessToken = ""
accessTokenSecret = ""

auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)

搜索:

query = 'blockchain'
max_tweets = 5
searched_tweets = [status for status in tweepy.Cursor(api.search, q=query).items(max_tweets)]

但是我不清楚如何从这里解析数据,变量searched_tweets是一个只有一个元素的列表:

print type(searched_tweets)
print len(searched_tweets)

<type 'list'>
5

第一个元素是tweepy.models.Status,但是没有得到如何解析。

"Status(contributors=None, truncated=False, text=u'RT @AdEx_Network: A shoutout to Richard Kastelein for mentioning the AdEx crowdsale #ico #tokensale #ad\u2026', is_quote_status=False, in_reply_to_status_id=None, id=884958221147938816L, favorite_count=0, _api=<tweepy.api.API object at 0xb25ec08c>, author=User(follow_request_sent=False, has_extended_profile=False, profile_use_background_image=True, _json={u'follow_request_sent': False, u'has_extended_profile': False, u'profile_use_background_image': True, u'default_profile_image': False, u'id': 846284378112090112L, u'profile_background_image_url_https': None, u'verified': False, u'translator_type': u'none', u'profile_text_color': u'333333', u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/873212431106834432/-QdMHr0M_normal.jpg', u'profile_sidebar_fill_color': u'DDEEF6', u'entities': {u'description': {u'urls': []}}, u'followers_count': 735, u'profile_sidebar_border_color': u'C0DEED', u'id_str': u'846284378112090112', u'profile_background_color': u'F5F8FA', u'listed_count': 0, u'is_translation_enabled': False, u'utc_offset': None, u'statuses_count': 100, u'description': u'', u'friends_count': 126, u'location': u'', u'profile_link_color': u'1DA1F2', u'profile_image_url': u'http://pbs.twimg.com/profile_images/873212431106834432/-QdMHr0M_normal.jpg', u'following': False, u'geo_enabled': False, u'profile_background_image_url': None, u'screen_name': u'AfiniChristy', u'lang': u'id', u'profile_background_tile': False, u'favourites_count': 100, u'name': u'Fitri Christy Afini', u'notifications'"

1 个答案:

答案 0 :(得分:1)

要从Status对象获取文本,您有两个选项。首先是简单地使用.text并获取列表中的推文文本,例如:

>>> searched_tweets[0].text
'RT @LiquidHub: The World Of #Cryptocurrency\n\n#Blockchain #Fintech #makeyourownlane #Mpgvip #AI #defstar5 #IOT #Bitcoin #GrowthHacking #Bigd…'

注意这是用&#34; ...&#34;截断。在推文的末尾,根据文档,这是因为它们实际上是转推,要从Status对象获取全文,您应该更改请求,然后使用.full_text

searched_tweets = [status for status in tweepy.Cursor(api.search, q=query, tweet_mode='extended').items(max_tweets)]

# Then you can print the full text tweets:
>>> searched_tweets[4].full_text
'RT @StakepoolCom: Crypto Percentages Up Over 100% Today Check it out! #cryptocurrency #steem #blockchain'