如何从 Python 中的 Twitter 流媒体 API 获取原始收藏夹计数以及每个用户的关注者计数



我正试图使用Python包TwitterAPI从公共推文流中提取两个跟踪关键字的单独数据。

理想情况下,我希望获得retweeted_status对象的原始收藏夹计数(而不是用户的status包装),但由于print(retweeted_status['favorite_count'])print(status['favorite_count'])总是返回零,因此很难做到这一点。

如果做不到这一点,我希望能够获得流中每个用户的关注人数。运行print(item)时,我可以在每条推文返回的完整json中看到一个名为"friends_count"的实体,但如果运行print(user['friends_count']),我会收到以下错误:

  Traceback (most recent call last):
  File "twitter.py", line 145, in <module>
    friends()
  File "twitter.py", line 110, in favourites
    print(user['friends_count'])
KeyError: 'friends_count'

这就是我目前的完整代码:

import sys
sys.path.append('/Library/Python/2.6/site-packages')
from TwitterAPI import TwitterAPI
import string

OAUTH_SECRET    = "foo"
OAUTH_TOKEN     = "foo"
CONSUMER_KEY    = "foo"
CONSUMER_SECRET = "foo"
def friends():
  TRACK_TERM = 'hello'
  api = TwitterAPI(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET)
  f = api.request('statuses/filter', {'track': TRACK_TERM})
  for user in f:
    print(user['friends_count'])

def favorite():
  TRACK_TERM = 'kanye'
  api = TwitterAPI(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET)
  h = api.request('statuses/filter', {'track': TRACK_TERM})
  for retweeted_item in h:
    print(retweeted_item['favorite_count'])
if __name__ == '__main__':
    try:
        friends()
        favorite()
    except KeyboardInterrupt:
        print 'nGoodbye!'

任何建议或信息都将不胜感激-我想我在语法中的某个地方犯了一个错误(我是Python初学者!),抛出了KeyErrors,但经过数小时的搜索,无论是从TwitterAPI包的文档还是Twitter API本身都无法确定它是什么。

编辑:当我运行for user in f print(user)时,这就是流API为单个用户的帖子返回的内容(对不起,我不知道如何使其更具可读性/包装Stack Overflow上的文本)-你可以看到"friends_count"one_answers"followers_count"都返回了一个数字,但我不知道如果不导致KeyError,如何单独打印它们。

{u'contributors': None, u'truncated': False, u'text': u'Hearing Kanye spit on a Drake beat is just really a lot for me!!!! I was not prepared!!', u'is_quote_status': False, u'in_reply_to_status_id': None, u'id': 719940912453853184, u'favorite_count': 0, u'source': u'<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>', u'retweeted': False, u'coordinates': None, u'timestamp_ms': u'1460482264041', u'entities': {u'user_mentions': [], u'symbols': [], u'hashtags': [], u'urls': []}, u'in_reply_to_screen_name': None, u'id_str': u'719940912453853184', u'retweet_count': 0, u'in_reply_to_user_id': None, u'favorited': False, u'user': {u'follow_request_sent': None, u'profile_use_background_image': True, u'default_profile_image': False, u'id': 247986350, u'verified': False, u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/715358123108601856/KM-OCY2D_normal.jpg', u'profile_sidebar_fill_color': u'DDEEF6', u'profile_text_color': u'333333', u'followers_count': 277, u'profile_sidebar_border_color': u'FFFFFF', u'id_str': u'247986350', u'profile_background_color': u'C0DEED', u'listed_count': 1, u'profile_background_image_url_https': u'https://pbs.twimg.com/profile_background_images/695740599/089d0a4e4385f2ac9cad05498169e606.jpeg', u'utc_offset': -25200, u'statuses_count': 6024, u'description': u'this is my part, nobody else speak', u'friends_count': 298, u'location': u'las vegas', u'profile_link_color': u'FFCC4D', u'profile_image_url': u'http://pbs.twimg.com/profile_images/715358123108601856/KM-OCY2D_normal.jpg', u'following': None, u'geo_enabled': True, u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/247986350/1454553801', u'profile_background_image_url': u'http://pbs.twimg.com/profile_background_images/695740599/089d0a4e4385f2ac9cad05498169e606.jpeg', u'name': u'princess laser tag', u'lang': u'en', u'profile_background_tile': True, u'favourites_count': 9925, u'screen_name': u'hannahinloafers', u'notifications': None, u'url': u'http://eecummingsandgoings.tumblr.com', u'created_at': u'Sun Feb 06 00:49:24 +0000 2011', u'contributors_enabled': False, u'time_zone': u'Pacific Time (US & Canada)', u'protected': False, u'default_profile': False, u'is_translator': False}, u'geo': None, u'in_reply_to_user_id_str': None, u'lang': u'en', u'created_at': u'Tue Apr 12 17:31:04 +0000 2016', u'filter_level': u'low', u'in_reply_to_status_id_str': None, u'place': None}

我已经解决了这个问题,并且认为这是我不了解如何从嵌套字典中检索JSON的问题。这起到了作用:

if 'retweeted_status' in item:
    item2 = item['retweeted_status']
    print(item2['favorite_count'])

最新更新