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')

 

posted @ 2017-06-21 18:26  my8100  阅读(972)  评论(0编辑  收藏  举报