How to get a person’s friends and followers on Twitter using the tweepy library?

Asked

Viewed 124 times

0

The function getting_friends_follwers() below works if I remove the value 100 from (cursor2.items(100)) . My goal is to take these names (followers and friends) and save in a file "friends.txt".

The problem: The name screen_name has a huge amount of friends and followers and thus the connection is terminated by Twitter. I thought about trying to capture the names from 100 to 100 (hence the value 100 in the cursor2 call) but the following error occurs:

builtins.TypeError: '<' not supported between instances of 'User' and 'User'

How to correct?

f = open("amigos.txt","w")
Meu = []
def getting_friends_follwers():
    # Get list of followers and following for group of users tweepy
    cursor = tweepy.Cursor(api.friends, screen_name="Carlos")
    cursor2 = tweepy.Cursor(api.followers, screen_name="Carlos")
##    for user in cursor.items():
##        print('friend: ' + user.screen_name)

    for user in sorted(cursor2.items(100)):###funciona se eu tirar este valor!!!
         f.write(str(user.screen_name)+ "\n")


         print('follower: ' + user.screen_name)

f.close()
getting_friends_follwers()
  • 1

    I don’t know the tweepy so well, but that mistake must be because of the sorted, because it cannot compare the users returned by cursor to order them

1 answer

0

Try this


f = open("amigos.txt","w")
user = '@Carlos'
count=300 # quantidade de seguidores que será retornado pela api (altere para quantidade que quiser, só cuidado com o limite da api)
   
try:
    followers = tweepy.Cursor(api.followers,screen_name=user).items(count)
                 
    for follower in followers:
         f.write(str(follower.screen_name)+ "\n")
        
except BaseException as e:
          print('failed on_status,',str(e))
          sleep(3)
f.close()

This should code does what you’re looking for, but the @carlos user has the private profile, and it’s not possible to collect your followers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.