5

私はカーソルクラスを使用したtweepyとpaginationにかなり慣れていません。特定のTwitterユーザーのすべてのフォロワーを取得するためにカーソルクラスを使用しようとしていますが、次のようなエラーが発生し続けます。"tweepy.error.TweepError: This method does not perform pagination" したがって、誰かがこのタスクを達成するのを手伝ってくれれば、 tweepyを使用して、ページネーションを行う特定のTwitterユーザー。私がこれまでに持っているコードは次のとおりです。

import tweepy

consumer_key='xyz'
consumer_secret='xyz'

access_token='abc'
access_token_secret='def'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)


user = api.get_user('somehandle')
print user.name

followers = tweepy.Cursor(user.followers)
temp=[]
for user in followers.items():
    temp.append(user)
    print temp
#the following part works fine but that is without pagination so I will be able to retrieve at #most 100 followers
aDict =  user.followers()
for friend in aDict:
    friendDict = friend.__getstate__()
    print friendDict['screen_name']
4

1 に答える 1

6

と呼ばれる便利な方法がありfollowers_idsます。指定された(または、、または)に対して最大5000のフォロワー(twitter api limit)IDを返します。screen_nameiduser_idcursor

次に、これらの結果をPythonで手動でページ分割lookup_usersし、すべてのチャンクを呼び出すことができます。lookup_users一度に100個のユーザーIDしか処理できない限り(twitter api limit)、チャンクサイズを100に設定するのはかなり論理的です。

コードは次のとおりです(ページネーションの部分はここから取得されました):

import itertools
import tweepy


def paginate(iterable, page_size):
    while True:
        i1, i2 = itertools.tee(iterable)
        iterable, page = (itertools.islice(i1, page_size, None),
                list(itertools.islice(i2, page_size)))
        if len(page) == 0:
            break
        yield page


auth = tweepy.OAuthHandler(<consumer_key>, <consumer_secret>)
auth.set_access_token(<key>, <secret>)

api = tweepy.API(auth)

followers = api.followers_ids(screen_name='gvanrossum')

for page in paginate(followers, 100):
    results = api.lookup_users(user_ids=page)
    for result in results:
        print result.screen_name

お役に立てば幸いです。

于 2013-05-04T21:47:45.710 に答える