30

是否有可能获得拥有超过一百万粉丝的帐户的完整粉丝列表,例如麦当劳?

我使用 Tweepy 并遵循代码:

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

我也试试这个:

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

但我总是收到“超出速率限制”错误,并且只有 5000 个关注者 ID。

4

4 回答 4

49

为了避免速率限制,您可以/应该在下一个关注者页面请求之前等待。看起来很hacky,但有效:

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)

希望有帮助。

于 2013-07-05T14:05:33.750 回答
28

建立连接时使用速率限制参数。该api将在速率限制内自我控制。

睡眠暂停还不错,我用它来模拟人类并在一个时间范围内分散活动,并将 api 速率限制作为最终控制。

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

还添加 try/except 以捕获和控制错误。

示例代码 https://github.com/aspiringguru/twitterDataAnalyse/blob/master/sample_rate_limit_w_cursor.py

我将密钥放在外部文件中,以便于管理。

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

于 2016-07-22T23:36:23.430 回答
5

我使用此代码,它适用于大量追随者:有两个功能,一个用于在每个睡眠期后保存追随者 ID,另一个用于获取列表:它有点小,但我希望有用。

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
于 2019-08-31T12:18:10.883 回答
3

alecxe 的答案很好,但是没有人提到文档。回答问题的正确信息和解释位于Twitter API 文档中。从文档中:

结果以 5,000 个用户 ID 为一组给出,并且可以在后续请求中使用 next_cursor 值浏览多个结果“页面”。

于 2018-07-09T09:51:08.317 回答