我需要获得拥有大约 125K 关注者的 Twitter 帐户的所有关注者。 所以我运行这段代码:
import tweepy
auth = tweepy.OAuth2AppHandler(api_key, api_secret)
api = tweepy.API(auth)
tweepy.Cursor(api.get_followers,screen_name=sN,count=100).items(125000)
凭据位于提升的开发者帐户的开发应用程序下。
我得到了这个错误:
TooManyRequests:429 太多请求 88 - 超出速率限制
有没有我可以用来请求 less 项目并获得 125000 关注者的分页器?我怎样才能用光标页面补充这段代码?
谢谢!
2023 年 4 月 22 日我运行这个:
auth = tweepy.OAuth1UserHandler(
trikini.api_key, trikini.api_secret
)
api = tweepy.API(auth, wait_on_rate_limit=True)
first_net = []
for status in tweepy.Cursor(api.get_followers, screen_name=sN,
count=200).items():
print(status.id)
first_net.append(status.id
#status.screen_name]
)
得到这个错误: 未授权:401 未授权 未授权。
然后我试了这个:
import tweepy
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret,
access_token, access_token_secret
)
api = tweepy.API(auth, wait_on_rate_limit=True)
first_net = []
for status in tweepy.Cursor(api.get_followers, screen_name=sN,
count = 200).items(125000):
print(status.screen_name)
ids.append([status.id,status.screen_name])
with open(r'filename.txt', 'w') as fp:
for item in ids:
fp.write("%s\n" % item)
first_net
代码执行结束,但我只得到了252个ID,用sN屏蔽的用户有112565个粉丝。可能发生了什么?
错误
TooManyRequests: 429 Too Many Requests 88 - Rate limit exceeded
被抛出,因为你超过了标准速率限制。
查看 Twitter API 速率限制,这与 tweepy 相同。
标准速率限制:
允许的最大请求数基于时间间隔、某些指定的时间段或时间窗口。最常见的请求限制间隔是十五分钟。如果端点的速率限制为 900 个请求/15 分钟,则在任何 15 分钟间隔内最多允许 900 个请求。
您可以使用一个参数 (wait_on_rate_limit) 来减轻错误。一旦您达到速率限制阈值,此参数将使您的查询会话进入睡眠模式。该参数旨在在速率限制阈值重新启动后建立会话。
这是它的使用方法。下面的参考来自代码库.
# Setting wait_on_rate_limit to True when initializing API will initialize
# an instance, called api here, that will automatically wait, using time.sleep,
# for the appropriate amount of time when a rate limit is #encountered
api = tweepy.API(auth, wait_on_rate_limit=True)
这是tweepy.API
的另一个
示例参考,下面的代码来自该参考:
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
# Setting wait_on_rate_limit to True when initializing API will initialize an
# instance, called api here, that will automatically wait, using time.sleep,
# for the appropriate amount of time when a rate limit is encountered
api = tweepy.API(auth, wait_on_rate_limit=True)
# This will search for Tweets with the query "Twitter", returning up to the
# maximum of 100 Tweets per request to the Twitter API
# Once the rate limit is reached, it will automatically wait / sleep before
# continuing
for tweet in tweepy.Cursor(api.search_tweets, "Twitter", count=100).items():
print(tweet.id)
这是一个占位符,用于解决您最后的代码注释:
稍后会删除
import tweepy
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret,
access_token, access_token_secret
)
api = tweepy.API(auth, wait_on_rate_limit=True)
twitter_ids = []
for status in tweepy.Cursor(api.get_followers, screen_name=sN,
count = 200).items(125000):
print(status.screen_name)
twitter_ids.append([status.id,status.screen_name])
# I moved this outside of the for loop, because it
# should be written to after all the IDs have been collected.
with open(r'filename.txt', 'w') as fp:
for item in ids:
fp.write("%s\n" % item)