Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract information from Tweepy ResultSet

Tags:

python

tweepy

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!

like image 393
Nate Avatar asked Mar 01 '17 21:03

Nate


People also ask

How to extract Twitter data from users?

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.

What is tweepy and how to use it?

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.

How do I retrieve data from Twitter using the API?

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.

How to authorize your app to access Twitter using tweepy?

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.


1 Answers

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)
like image 110
alecxe Avatar answered Nov 27 '22 10:11

alecxe