爬取Python3.9 中文版教程链接代码
目标地址:https://docs.python.org/zh-cn/3.9/tutorial/index.html
爬取内容:中文版教程的标题和链接
起因
很多初学者小白都在想,直接把官方的链接下载下来,随时可以转换pdf、chm等等,也方便编辑使用。
1.导入库
import requests
from pyquery import PyQuery as pq
2.请求
url = ‘http://docs.python.org/zh-cn/3.9/tutorial/index.html’
html = requests.get(url)
3.规范编码
html.encoding = ‘utf-8’
4.遍历标题和链接
for new in news:
print(new.text())
print(new.attr.href)
5.完整代码
import requests
from pyquery import PyQuery as pq
url = 'http://docs.python.org/zh-cn/3.9/tutorial/index.html'
html = requests.get(url)
html.encoding = 'utf-8'
doc = pq(html.text)
news = doc('.toctree-wrapper ul a').items()
for new in news:
print(new.text())
print(new.attr.href)
6.结果
1. 课前甜点
appetite.html
2. 使用 Python 解释器
interpreter.html
2.1. 调用解释器
interpreter.html#invoking-the-interpreter
2.1.1. 传入参数
interpreter.html#argument-passing
2.1.2. 交互模式
interpreter.html#interactive-mode
2.2. 解释器的运行环境
interpreter.html#the-interpreter-and-its-environment
2.2.1. 源文件的字符编码
interpreter.html#source-code-encoding
3. Python 的非正式介绍
introduction.html
3.1. Python 作为计算器使用
introduction.html#using-python-as-a-calculator
3.1.1. 数字
introduction.html#numbers
3.1.2. 字符串
introduction.html#strings
3.1.3. 列表
introduction.html#lists
3.2. 走向编程的第一步
introduction.html#first-steps-towards-programming
4. 其他流程控制工具
controlflow.html
4.1. if 语句
controlflow.html#if-statements
4.2. for 语句
controlflow.html#for-statements
4.3. range() 函数
controlflow