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!
Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from twitter developer available easily for each user.
Here Tweepy is introduced as a tool to access Twitter data in a fairly easy way with Python. There are different types of data we can collect, with the obvious focus on the “tweet” object.
For using the Twitter API you need to have a developer access Twitter account. Request for the same it might take 2–3 hours to get an approval. Once, you’re done with the set up create an app, in it, you will get Keys and tokens, which will help us retrieve data from Twitter. They act as login credentials.
Now in order to authorize our app to access Twitter on our behalf, we need to use the OAuth Interface. Tweepy provides the convenient Cursor interface to iterate through different types of objects. Twitter allows a maximum of 3200 tweets for extraction.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With