Get All Follower IDs in Twitter by Tweepy

核能气质少年 提交于 2019-11-26 10:35:00

问题


Is it possible to get the full follower list of an account who has more than one million followers, like McDonald\'s?

I use Tweepy and follow the code:

c = tweepy.Cursor(api.followers_ids, id = \'McDonalds\')
ids = []
for page in c.pages():
     ids.append(page)

I also try this:

for id in c.items():
    ids.append(id)

But I always got the \'Rate limit exceeded\' error and there were only 5000 follower ids.


回答1:


In order to avoid rate limit, you can/should wait before the next follower page request. Looks hacky, but works:

import time
import tweepy

auth = tweepy.OAuthHandler(..., ...)
auth.set_access_token(..., ...)

api = tweepy.API(auth)

ids = []
for page in tweepy.Cursor(api.followers_ids, screen_name="McDonalds").pages():
    ids.extend(page)
    time.sleep(60)

print len(ids)

Hope that helps.




回答2:


Use the rate limiting arguments when making the connection. The api will self control within the rate limit.

The sleep pause is not bad, I use that to simulate a human and to spread out activity over a time frame with the api rate limiting as a final control.

api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True)

also add try/except to capture and control errors.

example code https://github.com/aspiringguru/twitterDataAnalyse/blob/master/sample_rate_limit_w_cursor.py

I put my keys in an external file to make management easier.

https://github.com/aspiringguru/twitterDataAnalyse/blob/master/keys.py




回答3:


The answer from alecxe is good, however no one has referred to the docs. The correct information and explanation to answer the question lives in the Twitter API documentation. From the documentation :

Results are given in groups of 5,000 user IDs and multiple “pages” of results can be navigated through using the next_cursor value in subsequent requests.




回答4:


I use this code and it works for a large number of followers : there are two functions one for saving followers id after every sleep period and another one to get the list : it is a little missy but I hope to be useful.

def save_followers_status(filename,foloowersid):
    path='//content//drive//My Drive//Colab Notebooks//twitter//'+filename
    if not (os.path.isfile(path+'_followers_status.csv')):
      with open(path+'_followers_status.csv', 'wb') as csvfile:
        filewriter = csv.writer(csvfile, delimiter=',')


    if len(foloowersid)>0:
        print("save followers status of ", filename)
        file = path + '_followers_status.csv'
        # https: // stackoverflow.com / questions / 3348460 / csv - file - written -with-python - has - blank - lines - between - each - row
        with open(file, mode='a', newline='') as csv_file:
            writer = csv.writer(csv_file, delimiter=',')
            for row in foloowersid:
                writer.writerow(np.array(row))
            csv_file.closed

def get_followers_id(person):
    foloowersid = []
    count=0

    influencer=api.get_user( screen_name=person)
    influencer_id=influencer.id
    number_of_followers=influencer.followers_count
    print("number of followers count : ",number_of_followers,'\n','user id : ',influencer_id)
    status = tweepy.Cursor(api.followers_ids, screen_name=person, tweet_mode="extended").items()
    for i in range(0,number_of_followers):
        try:
            user=next(status)
            foloowersid.append([user])
            count += 1
        except tweepy.TweepError:
            print('error limite of twiter sleep for 15 min')
            timestamp = time.strftime("%d.%m.%Y %H:%M:%S", time.localtime())
            print(timestamp)
            if len(foloowersid)>0 :
                print('the number get until this time :', count,'all folloers count is : ',number_of_followers)
                foloowersid = np.array(str(foloowersid))
                save_followers_status(person, foloowersid)
                foloowersid = []
            time.sleep(15*60)
            next(status)
        except :
            print('end of foloowers ', count, 'all followers count is : ', number_of_followers)
            foloowersid = np.array(str(foloowersid))
            save_followers_status(person, foloowersid)      
            foloowersid = []
    save_followers_status(person, foloowersid)
    # foloowersid = np.array(map(str,foloowersid))
    return foloowersid


来源:https://stackoverflow.com/questions/17431807/get-all-follower-ids-in-twitter-by-tweepy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!