【原创】爬取Python3.9 中文版教程链接代码----文科生的python自学之路(4)

本文介绍如何爬取Python3.9的官方中文教程链接,通过导入requests和PyQuery库,请求网页,规范编码,遍历并打印标题和链接,帮助初学者方便地下载教程。
摘要由CSDN通过智能技术生成

爬取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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值