作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经使用 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/
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 6年前关闭。 Improve this questi
我是一名优秀的程序员,十分优秀!