【发布时间】:2016-08-03 11:11:03
【问题描述】:
我正在尝试使用 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' 和 '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}
【问题讨论】:
-
我不认为这是语法错误,但我不知道这个 API。
KeyError表示您正在尝试访问不存在的东西。你在for user in f: print user中看到了什么开始调试?您需要提供我认为的 json,或者至少在示例中提供其结构。这也可以解释为什么您尝试使用status得到的结果为零 -
@roganjosh 我添加了一个示例,说明当我在 f: print user 中为用户运行时会发生什么 - 希望这能揭示一些东西!
-
好的,越来越近了。编辑不是有效的 json,所以我不确定为什么 API 会返回类似的东西。我想弄清楚;编辑显示单个
user? -
您可以尝试以下方法吗?首先
import json然后for user in f: a= json.dumps(user) \n print (a)并给出一个样本?您的示例中单引号和双引号的混合使我很难知道我正在做的任何事情是否有效。当前返回的不是 json,但也许您可以立即转换它。 (\n 意思是放在下一行) -
全部解决,谢谢!
标签: python twitter twitter-streaming-api