0

I currently use the function get_users_following(user_id_account, max_results=1000) to get the list of follows of an account, to know who he follows on twitter. So far it works well as long as the user follows less than 1000 people because the API limits to a list of maximum 1000 users. The problem is that when he follows more than 1000 people I can't get the last people. The function always gives me the first 1000 and ignores the last ones

https://docs.tweepy.org/en/stable/client.html#tweepy.Client.get_users_followers

https://developer.twitter.com/en/docs/twitter-api/users/follows/api-reference/get-users-id-following

There is a pagination_token parameter but I don't know how to use it. What I want is just the last X new people followed so I can add them to a database and get a notification for each new entry

client = tweepy.Client(api_token)
response = client.get_users_following(id=12345, max_results=1000)

Is it possible to go directly to the last page?

Miky
  • 109
  • 2
  • 10

1 Answers1

1

Tweepy handles the pagination with the Paginator class (see the documentation here).

For example, if you want to see all the pages, you could do something like that:

# Use the wait_on_rate_limit argument if you don't handle the exception yourself
client = tweepy.Client(api_token, wait_on_rate_limit=True)

# Instantiate a new Paginator with the Tweepy method and its arguments
paginator = tweepy.Paginator(client.get_users_following, 12345, max_results=1000)

for response_page in paginator:
    print(response_page)

Or you could also directly get the full list of the user's followings:

# Instantiate a new Paginator with the Tweepy method and its arguments
paginator = tweepy.Paginator(client.get_users_following, 12345, max_results=1000)

for user in paginator.flatten():  # Without a limit argument, it gets all users
    print(user.id)
Mickaël Martinez
  • 1,794
  • 2
  • 7
  • 20