Tweepy 使用多个 API 键和光标来搜索 Twitter
Tweepy use multiple API keys with cursor to search Twitter
我一直在使用这个 post
中的示例
创建一个在短时间内搜索并获取大量推文的系统。但是,每次我切换到一个新的 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
我找到了一个我认为可行的解决方法,尽管我仍然遇到一些问题。思路是加入
currentCursor = tweepy.Cursor(apis[currentAPI].search, q = '%40deltaKshatriya', cursor = currentCursor, max_id = max_id)
其中 max_id 是在达到速率限制之前获取的最后一条推文的 ID。我遇到的唯一问题是很早就提出了 StopIteration(在我收到完整的 100,000 条推文之前),但我认为这是一个不同的 SO 问题。
我一直在使用这个 post
中的示例创建一个在短时间内搜索并获取大量推文的系统。但是,每次我切换到一个新的 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
我找到了一个我认为可行的解决方法,尽管我仍然遇到一些问题。思路是加入
currentCursor = tweepy.Cursor(apis[currentAPI].search, q = '%40deltaKshatriya', cursor = currentCursor, max_id = max_id)
其中 max_id 是在达到速率限制之前获取的最后一条推文的 ID。我遇到的唯一问题是很早就提出了 StopIteration(在我收到完整的 100,000 条推文之前),但我认为这是一个不同的 SO 问题。