8

How do I interact with the Tweepy Result set? How can I extract information? It kinda looks like a list or a dictionary, but I'm having trouble extracting specific elements of it.

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[...]

I've never dealt with an object like ResultSet before, so I'm not sure how I can extract information from it. I noticed that it works a bit like a list, in that I can get a specific tweet from the list like this:

print(api.favorites('twitter')[1])

But I cant get embedded elements like this:

print(api.favorites('twitter')[1][0])
Out[3]: TypeError: 'Status' object does not support indexing

or like this:

print(api.favorites('twitter')[1]['favorited'])
Out[4]: TypeError: 'Status' object is not subscriptable

Any help is appreciated!

Nate
  • 2,113
  • 5
  • 19
  • 25

2 Answers2

6

tweepy.ResultSet is a subclass of a Python's list:

class ResultSet(list):
    """A list like object that holds results from a Twitter API query."""

The elements in the ResultSet can be of different types - statuses, users, relations etc.

Every element in your particular ResultSet is a Status instance, which allows the attribute access (via dot notation) to the twitter status properties:

result_set = api.favorites('twitter')
status = result_set[0]
print(status.favorited)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks a bunch! Is there a way to find all the elements of the object that I can reference with the dot notation? Currently, I'm just looking through the printed output and taking note, but I figure there's got to be a better way. – Nate Mar 01 '17 at 21:48
  • @Nate sure, I think the full underlying JSON object is available at `status._json`.. – alecxe Mar 01 '17 at 21:51
  • Thanks again @alecxe. For anyone looking for a quick answer: status._json.keys() – Nate Mar 01 '17 at 22:38
0

Here i displayed the resultset using for loop. And inside for loop, use the same way as @alecxe mentioned to access each object property

# 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 ''

Output:

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 - 49813707
Rohan Khude
  • 4,455
  • 5
  • 49
  • 47