如何与Tweepy结果集交互?我如何提取信息?它看起来有点像一个列表或字典,但我在提取其中的特定元素时遇到了困难。
import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
print(type(api.favorites('twitter')))
Out[1]: <class 'tweepy.models.ResultSet'>
print(api.favorites('twitter'))
Out[2]: Status(favorited=False, source='Twitter for iPhone', in_reply_to_status_id=None, coordinates=None, text='Starting the Twitter chat now. https://t.co[...]我以前从未处理过像ResultSet这样的对象,所以我不知道如何从它中提取信息。我注意到它有点像一个列表,因为我可以从列表中得到一个特定的推特,如下所示:
print(api.favorites('twitter')[1])但我不能得到这样的嵌入式元素:
print(api.favorites('twitter')[1][0])
Out[3]: TypeError: 'Status' object does not support indexing或者像这样:
print(api.favorites('twitter')[1]['favorited'])
Out[4]: TypeError: 'Status' object is not subscriptable任何帮助都是非常感谢的!
发布于 2017-03-01 21:29:54
发布于 2017-12-18 19:51:25
在这里,我使用resultset循环显示了for。在for循环中,使用与@alecxe相同的方式访问每个对象属性。
# For ex, GET users/lookup API call returns resultset
users = api.lookup_users(screen_names=['StackOverflow,StackExchange'])
# get the length of user and @alecxe mentions
for i in range(len(users)):
print 'Name - ' + users[i].name
print 'Bio - ' + users[i].description
print 'Location - ' + users[i].location
print 'Joined at - ' + str(users[i].created_at)
print 'User ID - ' + users[i].id_str
print ''输出:
Name - TechCrunch
Bio - Breaking technology news, analysis, and opinions from TechCrunch. Home to Disrupt, TC Sessions, and Startup Battlefield. Got a tip? tips@techcrunch.com
Location - San Francisco, CA
Joined at - 2007-03-07 01:27:09
User ID - 816653
Name - Piwik Analytics
Bio - Piwik is the leading open analytics platform (Web+Mobile). An open alternative to Google Analytics. Privacy is built-in. Tweet about Piwik if you love it!
Location - Planet Earth
Joined at - 2009-06-22 23:47:00
User ID - 49813707https://stackoverflow.com/questions/42542327
复制相似问题