Skip to content

Commit 47b6370

Browse files
committed
feat: 07_Python常见的网络请求库.md done
1 parent 3c626fe commit 47b6370

File tree

3 files changed

+156
-1
lines changed

3 files changed

+156
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
- [x] [04_爬虫的基本工作原理](爬虫入门/04_爬虫的基本工作原理.md)
2727
- [x] [05_常用的抓包工具有那些](爬虫入门/05_常用的抓包工具有那些.md)
2828
- [x] [06_为什么说用Python写爬虫有天生优势](爬虫入门/06_为什么说用Python写爬虫有天生优势.md)
29-
- [ ] [07_Python常见的网络请求库](爬虫入门/07_Python常见的网络请求库.md)
29+
- [x] [07_Python常见的网络请求库](爬虫入门/07_Python常见的网络请求库.md)
3030
- [ ] [08_爬虫入门实战1_静态网页数据提取](爬虫入门/08_爬虫入门实战1_静态网页数据提取.md)
3131
- [ ] [09_爬虫入门实战2_动态数据提取](爬虫入门/09_爬虫入门实战2_动态数据提取.md)
3232
- [ ] [10_爬虫入门实战3_数据存储实现](爬虫入门/10_爬虫入门实战3_数据存储实现.md)

源代码/爬虫入门/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,14 @@
22
# @Author : [email protected]
33
# @Time : 2024/3/24 16:35
44
# @Desc :
5+
6+
import httpx
7+
import asyncio
8+
9+
async def post_data():
10+
data = {'name': '程序员阿江','email':'[email protected]'}
11+
async with httpx.AsyncClient() as client:
12+
response = await client.post('https://httpbin.org/post', data=data)
13+
print(response.json())
14+
15+
asyncio.run(post_data())
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
### 1. 常用的网络请求库
2+
3+
在Python中,进行网络请求的库主要分为同步和异步两大类。
4+
5+
- **同步请求库**:
6+
- `urllib`: Python的标准库之一,提供了一系列用于操作URL的功能。
7+
- `requests`: 第三方库,提供了更加方便的API来发送HTTP请求,是最受欢迎的HTTP客户端库之一。
8+
9+
- **异步请求库**:
10+
- `aiohttp`: 支持异步请求的库,使用`asyncio`进行网络通信,适合处理高并发需求。
11+
- `httpx`: 是一个全功能的HTTP客户端,支持HTTP/1.1和HTTP/2,并且同时支持同步和异步接口。
12+
13+
### 2. 优缺点及适用场景
14+
15+
- `urllib`:
16+
- **优点**: 标准库,不需要额外安装。
17+
- **缺点**: API相对繁琐。
18+
- **适用场景**: 简单的应用,或不想引入外部依赖时。
19+
20+
- `requests`:
21+
- **优点**: API简单易用,社区支持强大。
22+
- **缺点**: 不支持异步。
23+
- **适用场景**: 大多数HTTP请求场景,尤其是对性能要求不是非常高的同步程序。
24+
25+
- `aiohttp`:
26+
- **优点**: 支持异步,适合高并发场景。
27+
- **缺点**: API相对复杂。
28+
- **适用场景**: 需要处理大量并发连接的应用。
29+
30+
- `httpx`:
31+
- **优点**: 同时支持同步和异步API,支持HTTP/2。
32+
- **缺点**: 相对较新,社区支持和稳定性正在增强中。
33+
- **适用场景**: 需要同时使用同步和异步请求,或需要HTTP/2支持的应用。
34+
35+
### 3. Requests和httpx的使用
36+
> headers、cookies、auth、proxy这几种是我们日常爬虫过程中,经常需要使用的,下面分别基于request和httpx来展示如何使用
37+
- **Requests**:
38+
39+
- **Headers**:
40+
```python
41+
import requests
42+
response = requests.get('https://httpbin.org/get', headers={'User-Agent': 'My App'})
43+
```
44+
45+
- **Cookies**:
46+
```python
47+
response = requests.get('https://httpbin.org/cookies', cookies={'session_id': '12345'})
48+
```
49+
50+
- **认证**:
51+
```python
52+
response = requests.get('https://httpbin.org/basic-auth/user/passwd', auth=('user', 'passwd'))
53+
```
54+
55+
- **SSL证书验证**:
56+
```python
57+
response = requests.get('https://httpbin.org/get', verify='/path/to/certfile')
58+
```
59+
60+
- **httpx**:
61+
62+
- **Headers**:
63+
```python
64+
import httpx
65+
response = httpx.get('https://httpbin.org/get', headers={'User-Agent': 'My App'})
66+
```
67+
68+
- **Cookies**:
69+
```python
70+
client = httpx.Client()
71+
client.cookies.set('session_id', '12345')
72+
response = client.get('https://httpbin.org/cookies')
73+
```
74+
75+
- **认证**:
76+
```python
77+
response = httpx.get('https://httpbin.org/basic-auth/user/passwd', auth=('user', 'passwd'))
78+
```
79+
80+
- **SSL证书验证**:
81+
```python
82+
response = httpx.get('https://httpbin.org/get', verify='/path/to/certfile')
83+
```
84+
85+
细心的你可能发现了,httpx和request的用法大差不差,是的没错。
86+
87+
httpx 的设计灵感来源于 requests,因此两者在用法上有很多相似之处。这是因为 httpx 的开发者希望提供一个类似于 requests 的简洁、易用的接口,同时又能够支持更多的功能和特性,比如对异步请求的支持以及对 HTTP/2 的原生支持。因此,如果您熟悉 requests 的用法,那么学习和使用 httpx 会变得非常容易和顺畅。
88+
89+
### 4. 入门示例
90+
91+
- **Requests示例**:
92+
93+
```python
94+
import requests
95+
96+
response = requests.get('https://httpbin.org/get')
97+
print(response.json())
98+
```
99+
100+
- **httpx示例**:
101+
102+
```python
103+
import httpx
104+
105+
response = httpx.get('https://httpbin.org/get')
106+
print(response.json())
107+
```
108+
109+
### 5. 实际业务逻辑示例
110+
111+
假设我们需要实现一个功能:向`httpbin.org/post`发送POST请求,提交一些数据,并接收响应。
112+
113+
- **使用Requests**:
114+
115+
```python
116+
import requests
117+
118+
data = {'name': '程序员阿江','email':'[email protected]'}
119+
response = requests.post('https://httpbin.org/post', data=data)
120+
print(response.json())
121+
```
122+
123+
- **使用httpx(异步)**:
124+
125+
```python
126+
import httpx
127+
import asyncio
128+
129+
async def post_data():
130+
data = {'name': '程序员阿江','email':'[email protected]'}
131+
async with httpx.AsyncClient() as client:
132+
response = await client.post('https://httpbin.org/post', data=data)
133+
print(response.json())
134+
135+
asyncio.run(post_data())
136+
```
137+
138+
### 6. 异步爬虫的趋势
139+
140+
Python3.7之后,随着`asyncio`库的成熟和普及,异步编程在Python中变得更加容易实现。异步爬虫可以同时发起和管理成百上千的网络请求,而不会阻塞主线程。这使得编写高性能的爬虫代码不再是难事,尤其是在数据采集、实时数据处理等领域,异步爬虫将会成为一种趋势。
141+
142+
### 7. 总结
143+
144+
在选择合适的网络请求库时,应考虑实际应用的需求:对于简单或不频繁的网络请求,可以选择`urllib``requests`;而在需要处理大量并发连接的场景下,则应考虑使用`aiohttp``httpx`。随着Python异步编程的发展,未来异步爬虫无疑会在性能和效率上带来更多优势。

0 commit comments

Comments
 (0)