gpt4 book ai didi

python - 普遍设置 tweepy useragent 吗?

转载 作者:行者123 更新时间:2023-12-01 05:56:17 25 4
gpt4 key购买 nike

我已经使用 tweepy 一段时间了,但我一直遇到速率限制问题,收到 429 错误。我知道您可以在单个调用上设置 header ,例如

api.get_user('twitter', headers={'User-Agent': 'MyUserAgent'})

但是有没有一种方法可以在一个地方设置 header ,而不必在每次 api 调用时都这样做?

最佳答案

黑客方式:

import functools
class NewAPI(object):
def __init__(self, api):
self.api = api
def __getattr__(self, key):
call = getattr(self.api, key)
@functools.wraps(call)
def wrapped_call(*args, **kwargs):
headers = kwargs.pop('headers', {})
headers['User-Agent'] = 'MyUserAgent' # or make this a class variable/instance variable
kwargs['headers'] = headers
return call(*args, **kwargs)
return wrapped_call

api = NewAPI(api)
print(api.get_user('twitter'))

免责声明:未经测试,因为我没有 tweepy。

关于python - 普遍设置 tweepy useragent 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12488613/

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