使用 Requests Python 包的钩子问题 [英] Problems with hooks using Requests Python package

查看:59
本文介绍了使用 Requests Python 包的钩子问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 requests 模块,当我开始使用钩子时,我收到了这条消息.

I'm using the module requests and I recieved this message when I started using hooks.

File "/Library/Python/2.7/site-packages/requests-1.1.0-py2.7.egg/requests/sessions.py", line 321, in request
resp = self.send(prep, **send_kwargs)

File "/Library/Python/2.7/site-packages/requests-1.1.0-py2.7.egg/requests/sessions.py", line 426, in send
r = dispatch_hook('response', hooks, r, **kwargs)

File "/Library/Python/2.7/site-packages/requests-1.1.0-py2.7.egg/requests/hooks.py", line 41, in dispatch_hook
_hook_data = hook(hook_data, **kwargs)
TypeError: hook() got an unexpected keyword argument 'verify'

这是我的代码(简化版):

And this is my code (simplified):

import requests
def hook(r):
     print r.json()

r = requests.get("http://search.twitter.com/search.json?q=blue%20angels&rpp=5", hooks=dict(response=hook))

推荐答案

根据请求文档,你的钩子函数不需要任何关键字参数,但根据 到github上的源代码,事件调度器可能会将kwargs传递给你的钩子函数.对我来说似乎是不完整的文档.将您的方法重新定义为:

According to the requests documentation, your hook function doesnt need to take any keyword arguments, but according to the source code on github, the event dispatcher may pass on kwargs to your hook function. Seems like incomplete documentation to me. Redefine your method as:

def hook(r, **kwargs):
    # ...

这篇关于使用 Requests Python 包的钩子问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆