我有这个代码
#!/usr/bin/python
import grequests
urls = [
'http://google.com',
'http://doesnotexists.tld'
]
def do_something(response, **kwargs):
print response.text
async_list = []
for u in urls:
action_item = grequests.get(u, timeout=10, hooks = {'response' : do_something})
async_list.append(action_item)
grequests.map(async_list,size=10)
如何处理错误而又不会收到通常的Python错误消息?
例如,对于不存在的域,它会打印出“未找到”。
看来grequests
从pypi安装(带有pip)不包含异常处理。但是github的版本实现了该功能:
def map(requests, stream=False, size=None, exception_handler=None)
因此,您应该克隆grequests或从github下载grequests.py并使用该版本。您可以使用pip直接安装该版本:
pip install git+https://github.com/kennethreitz/grequests.git
如果要查找异常处理示例,则可以查看存储库中的test.py。https://github.com/kennethreitz/grequests/blob/master/tests.py
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句