urllib设置debuglevel打开调试开关
生活随笔
收集整理的这篇文章主要介绍了
urllib设置debuglevel打开调试开关
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
1. 参考
Turning on debug output for python 3 urllib
https://bugs.python.org/issue26892
Python爬虫入门四之Urllib库的高级用法 5.使用DebugLog
2. 代码
(1) python2
1 # python2 2 import httplib 3 import urllib 4 httplib.HTTPConnection.debuglevel = 1 5 response = urllib.urlopen('http://www.baidu.com').read()
(2) 兼容python2和python3
1 import sys 2 try: 3 import urllib.request as urllib_request #python3 4 except: 5 import urllib2 as urllib_request #python2 6 print(sys.version) 7 httpHandler = urllib_request.HTTPHandler(debuglevel=1) 8 httpsHandler = urllib_request.HTTPSHandler(debuglevel=1) 9 opener = urllib_request.build_opener(httpHandler, httpsHandler) 10 print(opener.open('https://httpbin.org/user-agent').read().decode('utf-8')) 11 #install_opener之后才能全局 12 urllib_request.install_opener(opener) 13 response = urllib_request.urlopen('http://www.baidu.com')
转载于:https://www.cnblogs.com/my8100/p/7061147.html
与50位技术专家面对面20年技术见证,附赠技术全景图总结
以上是生活随笔为你收集整理的urllib设置debuglevel打开调试开关的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: PHP单粒模式
- 下一篇: 导出全部记录到excel