Tweepy 多重身份验证处理程序

编程入门 行业动态 更新时间:2024-10-28 18:26:45
本文介绍了Tweepy 多重身份验证处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 Tweepy 的 nirgs 分叉版本,我需要使用它来获取 2017-01-31 和 2017-02-01.我的代码有效,并且由于 Twitter 的速率限制,我必须在多个身份验证处理程序之间切换才能处理日期与前面提到的一样远的推文.我正在使用 此指令,但在达到速率限制并尝试切换到下一个使用 api.auth_idx += 1 验证处理程序我收到以下错误:

I am using nirgs forked version of Tweepy, which I need to use to get tweets between 2017-01-31 and 2017-02-01. My code works and due to Twitters Rate Limits I have to switch between multiple auth handlers to be able to work with tweets which dates are as far away as mentioned before. I am using this instruction, but after reaching Rate Limit and trying to switch to the next auth handler by using api.auth_idx += 1 I get the following error:

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

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

I don't know what I am doing wrong. Any help is appreciated a lot!

推荐答案

这是解决方案.

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

The error was indexing the object itself. I had to create a variable as an index:

switch += 1 api.auth_idx = switch

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

And here is the full code, in case anybody tries to build the same app:

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)

更多推荐

Tweepy 多重身份验证处理程序

本文发布于:2023-11-26 10:53:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633741.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   程序   Tweepy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!