使用Tweepy和游标搜索Twitter:多个API密钥的应用

3
我一直在使用这个帖子中的示例来创建一个系统,在短时间内搜索并获取大量推文。 但是,每次我切换到新的API密钥(创建新的光标),搜索就会从头开始,并且会得到重复的推文。 如何使每个光标从上一个离开的位置开始?我错过了什么? 这是我正在使用的代码:
currentAPI = 0

a = 0
currentCursor = tweepy.Cursor(apis[currentAPI].search, q = '%40deltaKshatriya')
c = currentCursor.items()
mentions = []
onlyMentions = []
while True:
    try:
        tweet = c.next()
        if a > 100000:
            break
        else:
            onlyMentions.append(tweet.text)
            for t in tTweets:
                if tweet.in_reply_to_status_id == t.id:
                    print str(a) + tweet.text
                    mentions.append(tweet.text)
        a = a + 1
    except tweepy.TweepError:
        print "Rate limit hit"
        if (currentAPI < 9):
            print "Switching to next sat in constellation"
            currentAPI =  currentAPI + 1
            #currentCursor = c.iterator.next_cursor
            currentCursor = tweepy.Cursor(apis[currentAPI].search, q = '%40deltaKshatriya', cursor = currentCursor)
            c = currentCursor.items()
        else:
            print "All sats maxed out, waiting and will try again"
            currentAPI = 0
            currentCursor = tweepy.Cursor(apis[currentAPI].search, q = '%40deltaKshatriya', cursor = currentCursor)
            c = currentCursor.items()
            time.sleep(60 * 15)
        continue
    except StopIteration:
        break

这只能在分叉的Tweepy版本和Python 2.7上工作,还是你以某种方式在Python 3上使用它? - sunwarr10r
我已经有一段时间没有使用这段代码了,但我认为我没有在Python 3中使用过它。我是在Mac预装的Python 2.7中使用的。 - deltaKshatriya
你知道我做错了什么吗?我正在尝试使用2.7版本,但是遇到了错误,这里是我的问题。当我尝试你的方法时,我也遇到了一个错误:TypeError: 'API' object does not support indexing - sunwarr10r
1
看起来你已经解决了。如果你还有问题,请告诉我。 - deltaKshatriya
1个回答

1
我找到了一个解决方法,我认为它有效,尽管我仍然遇到一些问题。思路是添加到代码中。
currentCursor = tweepy.Cursor(apis[currentAPI].search, q = '%40deltaKshatriya', cursor = currentCursor, max_id = max_id)

其中max_id是在达到速率限制之前获取的最后一条推文的id。我遇到的唯一问题是StopIteration被提前触发(在我获取完全部100,000条推文之前),但我认为这是一个不同的SO问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接