urllib设置debuglevel打开调试开关
生活随笔
收集整理的這篇文章主要介紹了
urllib设置debuglevel打开调试开关
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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')?
轉(zhuǎn)載于:https://www.cnblogs.com/my8100/p/7061147.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的urllib设置debuglevel打开调试开关的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP单粒模式
- 下一篇: 导出全部记录到excel