Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tweepy api.user_timeline: count limited to 200

seems that with tweepy I can get only 200 tweets using user_timeline method.

class Twitter_User():
    def __init__(self,id,count=200):
        self.id = id
        self.count = count
        self.data = None
    def get_tweets(self):
        store_tweets = api.user_timeline(self.id, count=self.count)
        simple_list = []
        for status in store_tweets:
            array = [status._json["text"].strip(), status._json["favorite_count"], status._json["created_at"],status._json["retweet_count"],[h["text"] for h in status._json["entities"]["hashtags"]]]
            simple_list.append(array)
        self.data = pd.DataFrame(simple_list, columns=["Text", "Like", "Created at","Retweet","Hashtags"])
        self.data = self.data[~self.data["Text"].str.startswith('RT')]
        return self.data
    def __repr__(self):
        id = api.get_user(self.id)
        return id.screen_name

If I put as self.count a number bigger than 200, I always will get a dataframe with 200 rows, instead if I put a smaller number, I get the correct amount of rows. I don't know, there is a limit or I have to use some other method?

like image 676
Giacomo Ciampoli Avatar asked Dec 14 '22 20:12

Giacomo Ciampoli


2 Answers

You can only get a maximum of 200 tweets in one request. However, you can make successive requests for older tweets. The maximum number of tweets that you can get in a timeline is 3200. The reference is here.

You can do this with tweepy but you will need to tweepy's Cursor get these successive pages of tweets. Look at this to get you started.

like image 138
Jonas Avatar answered Dec 30 '22 20:12

Jonas


To get more then 200, you need to use the cursor on user_timeline and then iterate over the pages.

import tweepy

# Consumer keys and access tokens, used for OAuth
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# Creation of the actual interface, using authentication
api = tweepy.API(auth)

for pages in tweepy.Cursor(api.user_timeline, id='id', count=200).pages():        
   print(pages)
like image 44
jjbskir Avatar answered Dec 30 '22 20:12

jjbskir