如何抑制从grequests获得的下列调试错误消息
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gevent/greenlet.py", line 327, in run
result = self._run(*self.args, **self.kwargs)
File "/usr/local/lib/python2.7/dist-packages/grequests.py", line 71, in send
self.url, **merged_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 456, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 559, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 384, in send
raise Timeout(e, request=request)
Timeout: HTTPSConnectionPool(host='itunes.apple.com', port=443): Read timed out.
<Greenlet at 0x7ff0c8165910: <bound method AsyncRequest.send of <grequests.AsyncRequest object at 0x7ff0c880b8d0>>(stream=False)> failed with Timeout以下是我迄今尝试过的:
# disable requests/grequests logging
logging.getLogger("requests").setLevel(logging.CRITICAL)
logging.getLogger("grequests").setLevel(logging.CRITICAL)
logging.getLogger("urllib3").setLevel(logging.CRITICAL)
logging.getLogger("gevent").setLevel(logging.CRITICAL)错误发生在这里:
rs = (grequests.get(url, headers=headers, cookies=cookies, proxies=proxies, timeout=timeout) for url in urls)
res_items = grequests.map(rs) # <-- this command produces all the errors我收到了上千条这样的信息,我不想看到。我还在很多其他地方使用log.debug,所以不想在程序范围内更改调试级别。如何仅抑制grequests调试消息?
发布于 2015-12-25 00:30:41
为此,您需要做三件事:
(1)确保安装了最新版本的grequests:
sudo pip install git+https://github.com/kennethreitz/grequests.git(2)为grequests.map函数设置一个处理程序:
def exception_handler(request, exception):
pass # suppress errors on grequests.map
res_items = grequests.map(rs, exception_handler=exception_handler)(3)抑制urllib3警告:
import urllib3
urllib3.disable_warnings()
logging.captureWarnings(True)现在,不应该有任何来自grequests的警告。
https://stackoverflow.com/questions/34458924
复制相似问题