requests 是什么
Requests 库是 Python 中最流行、最优雅的 HTTP 客户端库,让 Python 代码发送 HTTP 请求变得极其简单。
在爬虫五步流程里,它负责第一步"发送 HTTP 请求"。
安装:
bash
pip install requests入门程序:获取 TIOBE 编程语言排行榜
目标:抓取 https://www.tiobe.com/tiobe-index/ 的编程语言排行榜。
动手前先做三件事:
- 查看网站的
robots.txt(https://www.tiobe.com/robots.txt),明确哪些资源允许抓取 - 安装 requests 库
- 编写代码,访问网站获取数据
python
import requests
from lxml import html
# 定义 url
target_url = "https://www.tiobe.com/tiobe-index/"
# 发送请求, 获取数据
response = requests.get(target_url)
# response.text 就是服务器返回的 HTML 源码(字符串)
document = html.fromstring(response.text)
# 解析表头(Xpath 语法,下一节细讲)
th_list = document.xpath("//*[@id='top20']/thead/tr/th/text()")
print(th_list)
# 解析表格中的数据(遍历每一行)
tr_list = document.xpath("//table[@id='top20']/tbody/tr")
for tr in tr_list:
td_list = tr.xpath("./td/text()")
print(td_list)逐段理解
| 代码 | 说明 |
|---|---|
requests.get(target_url) | 向目标网址发 GET 请求,返回一个 Response 对象 |
response.text | 服务器返回的 HTML 源码(字符串) |
html.fromstring(response.text) | 把 HTML 字符串解析成"文档对象",之后才能用 Xpath 查询(来自 lxml 库) |
document.xpath("...") | 用 Xpath 表达式在文档里定位元素、取文本 |
//table[@id='top20'] | 任意位置找 id 为 top20 的 table([@属性='值'] 是 Xpath 的属性过滤) |
tr.xpath("./td/text()") | 在当前 tr 节点下找直接子元素 td 并取文本(./ 表示当前节点) |
WARNING
正式项目里给请求加超时:requests.get(url, timeout=60)。不加的话,对方服务器没响应时你的程序会一直卡着。
相关
练习题
一、知识回顾(读完直接做下面的实践题)
- Requests 库是 Python 中最流行、最优雅的 HTTP 客户端库
- 安装命令:
pip install requests - 发 GET 请求:
response = requests.get(url) - 拿到 HTML 源码字符串:
response.text - 把 HTML 字符串解析成文档对象:
html.fromstring(response.text)(来自 lxml 库) - 在文档对象上用 Xpath 语法定位元素、取文本
- Xpath 里
//*[@id='top20']的意思是:从任意位置找 id 为 top20 的任意元素 tr.xpath("./td/text()")里的./表示从当前节点下查找
二、裸写题
- 提示(先自己想,实在想不出再点开)
一级 · 思路:发请求 → 拿到响应 → 字符串切片 二级 · 方法:
requests.get(url)/response.text三级 · 骨架:print(response.text[:____])- 参考答案(做完再点开)
python# 2-1 import requests response = requests.get("https://www.itcast.cn", timeout=60) print(response.text[:200])- 提示(先自己想,实在想不出再点开)
一级 · 思路:请求 → 解析成文档对象 → Xpath 取表头 二级 · 方法:
html.fromstring/document.xpath("//*[@id='top20']/thead/tr/th/text()")三级 · 骨架:response = requests.get(url, timeout=____)- 参考答案(做完再点开)
python# 2-2 import requests from lxml import html response = requests.get("https://www.tiobe.com/tiobe-index/", timeout=60) document = html.fromstring(response.text) th_list = document.xpath("//*[@id='top20']/thead/tr/th/text()") print(th_list)- 提示(先自己想,实在想不出再点开)
一级 · 思路:网络请求随时可能失败(超时、连不上、域名不存在),失败时要有兜底 二级 · 方法:
try ... except Exception as e(第 25 篇异常处理) 三级 · 骨架:except Exception as e:\n print(f"请求失败: {e}")- 参考答案(做完再点开)
python# 2-3 import requests from lxml import html try: response = requests.get("https://www.tiobe.com/tiobe-index/", timeout=60) document = html.fromstring(response.text) print(document.xpath("//*[@id='top20']/thead/tr/th/text()")) except Exception as e: print(f"请求失败: {e}")
