python爬虫006-使用debuglog边运行边打印调试日志

# 希望在运行时,边运行边打印调试日志,此时需要开启DebugLog
import urllib.request

# (1)使用HTTPHander和HTTPSHander将debuglevel的值设置为1
httphd = urllib.request.HTTPHandler(debuglevel=1)
httpshd = urllib.request.HTTPSHandler(debuglevel=1)

# (2)build_opener创建自定义的opener对象,并用(1)中的值作为参数
opener = urllib.request.build_opener(httphd,httpshd)

# (3)install_opener创建全局默认的opener对象
# 这样使用urlopen时会使用我们安装的opener
urllib.request.install_opener(opener)

# (4)后续正常操作
data = urllib.request.urlopen("http://edu.51cto.com")

猜你喜欢

转载自www.cnblogs.com/buzhihuoyu/p/12456143.html