首页 > 编程语言 > 详细

python3 urllib使用debug输出

时间:2015-03-25 14:05:28      阅读:377      评论:0      收藏:0      [点我收藏+]

python2.7.5中使用debug输出,可以采用如下方式:

import urllib2  
httpHandler = urllib2.HTTPHandler(debuglevel=1)  
httpsHandler = urllib2.HTTPSHandler(debuglevel=1)  
opener = urllib2.build_opener(httpHandler, httpsHandler)  
urllib2.install_opener(opener)  
response = urllib2.urlopen('http://www.baidu.com')


python3 中统一使用的是urllib模块库,将python2中的urllib和urllib2进行了整合,试图按上述方式编写代码如下(python3.4.2 window7/cmd):

import urllib.request  

httpHandler = urllib.request.HTTPHandler(debuglevel=1)  
httpsHandler = urllib.request.HTTPSHandler(debuglevel=1)  
#opener = urllib.request.build_opener(httpHandler)  
urllib.request.install_opener(opener)  
response = urllib.request.urlopen('http://www.baidu.com')
with open("url_response.txt", "w") as f:
	print (response.read(), file=f)

没有语法错误提示,但是,也没有任何调试信息出来。。。

还有另外一种方式,使用http.client模块,代码如下:

import urllib.request  
import http.client

http.client.HTTPConnection.debuglevel = 1	

response = urllib.request.urlopen('http://www.baidu.com')
with open("url_response.txt", "w") as f:
	print (response.read(), file=f)

显然这种方式是有违原意的,不过还是出来了调试结果,如下:

技术分享

至于如何使用urllib模块实现此功能,还有待了解。

若有了解的,望指点一二~~

python3 urllib使用debug输出

原文:http://blog.csdn.net/haoxizh/article/details/44619497

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!