gpt4 book ai didi

python - Tweepy 多重身份验证处理程序

转载 作者:行者123 更新时间:2023-12-01 03:19:26 28 4
gpt4 key购买 nike

我正在使用nirgs forked version of Tweepy ,我需要用它来获取 2017-01-312017-02-01 之间的推文。我的代码可以工作,并且由于 Twitter 的速率限制,我必须在多个身份验证处理程序之间切换,以便能够处理日期与前面提到的相距很远的推文。我正在使用this instruction ,但在达到速率限制并尝试使用 api.auth_idx += 1 切换到下一个身份验证处理程序后,我收到以下错误:

File "build/bdist.macosx-10.11-intel/egg/tweepy/api.py", line 45, in auth_idx
IndexError: Index out of bounds

主要代码如下所示:

oauth_keys = [["consumer_key_1", "consumer_secret_1", "access_token_1", "access_token_secret_1"], ["consumer_key_2", "consumer_secret_2", "access_token_2", "access_token_secret_2"]]

auths = []
for consumer_key, consumer_secret, access_key, access_secret in oauth_keys:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
auths.append(auth)

api = tweepy.API(auths, monitor_rate_limit=True)

while True:
try:
for tweet in tweepy.Cursor(api.search, q='bitcoin', since=datetime.date(2017, 1, 31), lang='en').items(3000):
...
except tweepy.TweepError as e:
api.auth_idx += 1
continue

我不知道我做错了什么。非常感谢任何帮助!

最佳答案

这是解决方案。

错误是对对象本身进行索引。我必须创建一个变量作为索引:

switch += 1
api.auth_idx = switch

这里是完整的代码,以防有人尝试构建相同的应用程序:

oauth_keys = [
["consumer_key_1", "consumer_secret_1", "access_token_1", "access_token_secret_1"],
["consumer_key_2", "consumer_secret_2", "access_token_2", "access_token_secret_2"]
]

auths = []
for consumer_key, consumer_secret, access_key, access_secret in oauth_keys:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
auths.append(auth)

# api
api = tweepy.API(auths)
currentCursor = tweepy.Cursor(api.search, q = 'bitcoin', since='2017-02-01', until='2017-02-02', lang='en')
c = currentCursor.items()

switch = 0
api.auth_idx = switch
tweets = []
early_dt_val = datetime.datetime.strptime('2017-01-31 22:34:48', '%Y-%m-%d %H:%M:%S')
later_dt_val = datetime.datetime.strptime('2017-02-01 01:25:30', '%Y-%m-%d %H:%M:%S')
maxId = 0

while True:
try:
#for tweet in tweepy.Cursor(api.search, q='bitcoin', since=datetime.date(2017, 1, 31), lang='en').items(3000)
for tweet in c:
tweets.append(tweet)
maxId = tweet.id

for tweet in reversed(tweets):
tweetTime = tweet.created_at
if tweetTime < later_dt_val and tweetTime > early_dt_val:
print('Date: ' + str(tweet.created_at))
print('Tweet: ' + tweet.text)

except tweepy.TweepError as e:
print e
print "---------------------------Rate limit exceeded.---------------------------"
print "switching keys..."
switch += 1
if switch > 4:
print "Limit reached"
break
else:
api.auth_idx = switch # naechster Key
currentCursor = tweepy.Cursor(api.search, q='bitcoin', since='2017-02-01', until='2017-02-02', lang='en', max_id=maxId)

关于python - Tweepy 多重身份验证处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42059191/

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