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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值