gpt4 book ai didi

python - 使用 tweepy 和多个 API key 获取 Twitter 关注者

转载 作者:太空狗 更新时间:2023-10-30 01:22:38 25 4
gpt4 key购买 nike

我有多个 Twitter 开发 key ,用于从用户名列表中获取关注者。我有两种方法可以做到这一点,但两者都有问题。第一个:

try:
....
for user in tweepy.Cursor(api.followers, screen_name=screenName).items():
....
except tweepy.TweepError as e:

errorCode = e.message[0]['code']
if errorCode == 88:
print "Rate limit exceeded."
rotateKeys()

这里的问题是,每次轮换 key 时,for 循环都会从头开始并再次开始获取关注者。我试图解决这个问题,但拆分了 for 循环:

try:
items = tweepy.Cursor(api.followers, screen_name=s).items()

然后我使用 next(items)

手动遍历它们

但是旋转 api key 不起作用,因为初始调用是使用第一个 API 代码完成的,并且将始终尝试使用该代码。

我需要一种方法来轮换 key 并从之前的左边继续。

最佳答案

我实际上不得不放弃 cursored 方法,转而手动设置下一个光标。这样做的好处是“非游标”方法返回上一个和下一个游标作为其功能的一部分。

以下是我如何实现您的目标(注意:可能按顺序添加 try/catch):

users = ['user_one', 'user_two', 'user_three']

current_profile = 9 # I HAVE TEN IN AN ARRAY

tweepy_api = get_api(auth_profiles[current_profile]) #A FUNCTION I CREATED TO REINITIALIZE API'S

for user in users:

next_cursor = -1 # START EVERY NEW USER RETRIEVAL WITH -1

print 'CURRENT USER:', user, 'STARTING CURSOR:', next_cursor

while next_cursor: # THAT IS, WHILE CURSOR IS NOT ZERO

print 'AUTH PROFILE', current_profile, 'CURRENT CURSOR:', next_cursor

# RETURNS A TUPLE WITH ELEMENT[0] A LIST OF IDS, ELEMENT [1][0] PREVIOUS CURSOR, AND ELEMENT[1][1] NEXT CURSOR
ids, cursors = tweepy_api.followers_ids(screen_name=user, count=5000, cursor=next_cursor)

next_cursor = cursors[1] # STORE NEXT CURSOR

# FUNCTION I CREATED TO GET STATUS FROM API.rate_limit_status()
status = get_rate_limit_status(tweepy_api, '/followers/ids')

print 'ID\'S RETRIEVED:', len(ids), 'NEXT CURSOR:', cursors[1], 'REMAINING:', status['remaining']

if not status['remaining']: # IF STATUS IS REMAINING IS ZERO

print ''
print 'RATE LIMIT REACHED'

if current_profile < len(auth_profiles) - 1: # IF THE CURRENT PROFILE IS LESS THAN NINE (IN MY CASE)

print 'INCREMENTING CURRENT PROFILE:', current_profile, '<', len(auth_profiles) - 1

current_profile += 1 # INCREMENT THE PROFILE

print 'CURRENT PROFILE:', current_profile

else: # ELSE, IT MUST EQUAL NINE (COULD BE NEG I SUPPOSE BUT...)

print 'RESETTING CURRENT PROFILE TO ZERO:', current_profile, '=', len(auth_profiles) - 1

current_profile = 0 # RESET CURRENT PROFILE TO THE BEGINNING

print 'CURRENT PROFILE:', current_profile

tweepy_api = get_api(auth_profiles[current_profile]) # GET NEW TWEEPY API WITH NEW AUTH
print ''

输出应该是这样的(为简单起见,我删除了一些打印语句):

CURRENT USER: user_one STARTING CURSOR: -1
AUTH PROFILE 9 CURRENT CURSOR: -1

ID'S RETRIEVED: 5000 NEXT CURSOR: 1594511885763407081 REMAINING: 14

ID'S RETRIEVED: 5000 NEXT CURSOR: 1582249691352919104 REMAINING: 0

RATE LIMIT REACHED
RESETTING CURRENT PROFILE TO ZERO: 9 = 9
CURRENT PROFILE: 0

ID'S RETRIEVED: 5000 NEXT CURSOR: 1580277475971792716 REMAINING: 14

ID'S RETRIEVED: 4903 NEXT CURSOR: 0 REMAINING: 7

CURRENT USER: user_two STARTING CURSOR: -1
AUTH PROFILE 0 CURRENT CURSOR: -1

ID'S RETRIEVED: 5000 NEXT CURSOR: 1592820762836029887 REMAINING: 6

ID'S RETRIEVED: 5000 NEXT CURSOR: 1592737463603654258 REMAINING: 0

RATE LIMIT REACHED
INCREMENTING CURRENT PROFILE: 0 < 9
CURRENT PROFILE: 1

作为旁注,如果您要使用游标版本,至少在 Tweepy 3.5.0 中,prev_cursor 和 next_cursor 存储在 cursor.iterator.next_cursor、cursor.iterator.prev_cursor 中。我认为 3.6.0 也是如此(参见 Cursor 中的 CursorIteratorcursor.py )

对我来说,cursor.page_iterator.next_cursor 返回:

AttributeError: 'Cursor' object has no attribute 'page_iterator'

关于python - 使用 tweepy 和多个 API key 获取 Twitter 关注者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20427674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com