gpt4 book ai didi

python - 使用 Tweepy 避免 Twitter API 限制

转载 作者:IT老高 更新时间:2023-10-28 22:09:40 26 4
gpt4 key购买 nike

我在 Stack Exchange 上的一些问题中看到,限制可能是每 15 分钟请求数的函数,还取决于算法的复杂性,只不过这并不复杂。

所以我使用这个代码:

import tweepy
import sqlite3
import time

db = sqlite3.connect('data/MyDB.db')

# Get a cursor object
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS MyTable(id INTEGER PRIMARY KEY, name TEXT, geo TEXT, image TEXT, source TEXT, timestamp TEXT, text TEXT, rt INTEGER)''')
db.commit()

consumer_key = ""
consumer_secret = ""
key = ""
secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(key, secret)

api = tweepy.API(auth)

search = "#MyHashtag"

for tweet in tweepy.Cursor(api.search,
q=search,
include_entities=True).items():
while True:
try:
cursor.execute('''INSERT INTO MyTable(name, geo, image, source, timestamp, text, rt) VALUES(?,?,?,?,?,?,?)''',(tweet.user.screen_name, str(tweet.geo), tweet.user.profile_image_url, tweet.source, tweet.created_at, tweet.text, tweet.retweet_count))
except tweepy.TweepError:
time.sleep(60 * 15)
continue
break
db.commit()
db.close()

我总是收到 Twitter 限制错误:

Traceback (most recent call last):
File "stream.py", line 25, in <module>
include_entities=True).items():
File "/usr/local/lib/python2.7/dist-packages/tweepy/cursor.py", line 153, in next
self.current_page = self.page_iterator.next()
File "/usr/local/lib/python2.7/dist-packages/tweepy/cursor.py", line 98, in next
data = self.method(max_id = max_id, *self.args, **self.kargs)
File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 200, in _call
return method.execute()
File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 176, in execute
raise TweepError(error_msg, resp)
tweepy.error.TweepError: [{'message': 'Rate limit exceeded', 'code': 88}]

最佳答案

对于在 Google 上偶然发现此内容的任何人,tweepy 3.2+ 为 tweepy.api 提供了额外的参数。类,特别是:

  • wait_on_rate_limit – 是否自动等待速率限制补充
  • wait_on_rate_limit_notify – 是否在 Tweepy 等待速率限制补充时打印通知

将这些标志设置为 True 会将等待委托(delegate)给 API 实例,这对于大多数简单的用例来说已经足够了。

关于python - 使用 Tweepy 避免 Twitter API 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21308762/

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