Python爬虫入门<五>—DebugLog

本文介绍了如何通过Python的urllib库开启DebugLog,帮助在爬虫开发过程中调试错误并打印运行日志。通过设置HTTPHandler和HTTPSHandler的debuglevel为1,并创建自定义opener对象,最终实现对网站请求过程的详细记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python爬虫入门<五>——DebugLog

标签:DebugLog

DebugeLog

1.首先,我们先先来了解一下DebugLog是什么?
我们经常说的BUG就是错误,Debug就是调试错误,LOG代表日志,所以,,,DebugLog就是调试日志,这个可以帮助我们在运行程序的过程中打印日志,接下来,我们就开始开启DebugLog:
1.使用urllib.request.HTTPHandler()和urllib.request.HTTPSHandler()将debuglevel是指为1;
2.使用urllib.request.build_opener()创建自定义opener对象,使用1中的值作为对象的参数
3.urllib.request.install_opener()创建全局默认的opener对象,我们使用urlopen()的时候,就会使用opener对象。
4.继续后面操作
代码:

import urllib.request

httphd = urllib.request.HTTPHandler(debuglevel=1)
httpshd = urllib.request.HTTPSHandler(debuglevel=1)

opener = urllib.request.build_opener(httphd,httpshd)
urllib.request.install_opener(opener)
data = urllib.request.urlopen("http://www.baidu.com")

结果:

C:\Users\Administrator\PycharmProjects\untitled2\venv\Scripts\python.exe F:/python/python代码/venv/python入门五.py
send: b'GET / HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: www.baidu.com\r\nUser-Agent: Python-urllib/3.6\r\nConnection: close\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date header: Content-Type header: Transfer-Encoding header: Connection header: Vary header: Set-Cookie header: Set-Cookie header: Set-Cookie header: Set-Cookie header: Set-Cookie header: Set-Cookie header: P3P header: Cache-Control header: Cxy_all header: Expires header: X-Powered-By header: Server header: X-UA-Compatible header: BDPAGETYPE header: BDQID header: BDUSERID 
Process finished with exit code 0

说明日志正在打印,DebugLog成功开启了

### Python 技术框架教程概览 #### Flask 框架学习指南 Flask 是一个轻量级的 Web 应用程序框架,适合初学者和小型项目。相关课程涵盖了从基础到高级的内容,包括但不限于 Python 入门爬虫开发、数据分析以及 Web 开发等方面的知识[^1]。 ```python from flask import Flask, render_template_string app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!' if __name__ == '__main__': app.run(debug=True) ``` #### Scrapy 爬虫框架介绍 Scrapy 是专为网页抓取而设计的强大工具,能够高效地完成大规模的数据采集工作。该框架支持异步操作,允许并发执行多个 HTTP 请求;同时提供了丰富的插件机制来扩展其功能[^2]。 ```python import scrapy class MySpider(scrapy.Spider): name = "example_spider" start_urls = ['http://www.example.com'] def parse(self, response): self.log('Visited %s' % response.url) ``` #### Tornado 安装与使用说明 Tornado 是另一个流行的异步网络库,在高并发场景下表现出色。对于希望构建实时应用或微服务架构的人来说尤为适用[^3]。 ```python import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") application = tornado.web.Application([ (r"/", MainHandler), ]) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.current().start() ``` #### Pytest 测试框架配置实例 Pytest 提供了一套简单易用却又极其灵活的单元测试解决方案。通过自定义配置文件 `pytest.ini` 或者 `.ini` 文件中的 `[tool.pytest-ini.options]` 部分设置全局选项[^4]。 ```ini [pytest] addopts = -v --tb=short testpaths = tests/ python_files = *_tests.py test_*.py python_classes = *Tests Test* python_functions = test_* check_* ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值