Skip to content

Commit 3250cb7

Browse files
committed
requests模块讲解
1 parent f44b377 commit 3250cb7

File tree

3 files changed

+64
-31
lines changed

3 files changed

+64
-31
lines changed

images/12306ssl错误.png

257 KB
Loading

images/使用代理的过程.png

100 KB
Loading

网络请求模块的使用.md

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ with open('image.png','wb') as f:
159159
- 让服务器以为不是同一个客户端在请求
160160
- 防止我们的真实地址被泄露,防止被追究
161161
- 使用代理的过程
162-
![]()
162+
163+
![](./images/使用代理的过程.png)
163164

164165
- 代理分类
165166
- 透明代理(Transparent Proxy):透明代理虽然可以直接“隐藏”你的IP地址,但是还是可以查到你是谁。
@@ -170,7 +171,9 @@ with open('image.png','wb') as f:
170171
>
171172
> 从使用的协议:代理ip可以分为http代理,https代理,socket代理等,使用的时候需要根据抓取网站的协议来选择
172173
173-
使用方式
174+
- 使用方式
175+
176+
```python
174177
# 导入模块
175178
import requests
176179
# 定义请求地址
@@ -188,14 +191,23 @@ proxies = {
188191
response = requests.get(url,headers=headers,proxies=proxies)
189192
# 获取响应的 html 内容
190193
html = response.text
191-
代码讲解
192-
发送请求时 proxies 参数设置代理
193-
194-
七、发送请求携带 Cookies
195-
使用方式
196-
直接在自定义请求头中携带 Cookie
197-
通过请求参数携带 Cookie 对象
198-
代码
194+
```
195+
196+
- 代码讲解
197+
198+
> 发送请求时 proxies 参数设置代理
199+
200+
#### 发送请求携带 Cookies
201+
- 使用方式
202+
203+
204+
> 直接在自定义请求头中携带 Cookie
205+
>
206+
> 通过请求参数携带 Cookie 对象
207+
208+
- 代码
209+
210+
```python
199211
# 导入模块
200212
import requests
201213
# 定义请求地址
@@ -214,36 +226,53 @@ cookies = {
214226
response = requests.get(url,headers=headers,cookies=cookies)
215227
# 获取响应的 html 内容
216228
html = response.text
217-
代码讲解
218-
发送请求时 cookies 参数携带 Cookies
229+
```
219230

220-
八、错误证书处理
221-
问题描述
231+
- 代码讲解
232+
233+
> 发送请求时 cookies 参数携带 Cookies
222234
235+
#### 错误证书处理
236+
- 问题描述
237+
![](./images/12306ssl错误.png)
223238

224-
使用方式
239+
- 使用方式
240+
241+
```python
225242
# 导入模块
226243
import requests
227244

228245
url = "https://www.12306.cn/mormhweb/"
229246
# 设置忽略证书
230247
response = requests.get(url,verify=False)
231-
代码讲解
232-
发送请求时 verify 参数设置为 False 表示不验证CA证书
248+
```
233249

234-
九、超时处理
235-
使用方式
250+
- 代码讲解
251+
252+
253+
> 发送请求时 verify 参数设置为 False 表示不验证CA证书
254+
255+
#### 超时处理
256+
- 使用方式
257+
258+
```python
236259
# 导入模块
237260
import requests
238261

239262
url = "https://www.baidu.com"
240263
# 设置忽略证书
241264
response = requests.get(url,timeout=5)
242-
代码讲解
243-
发送请求时 timeout 参数设置为超时秒数
265+
```
266+
267+
- 代码讲解
268+
244269

245-
十、重试处理
246-
使用方式
270+
> 发送请求时 timeout 参数设置为超时秒数
271+
272+
#### 重试处理
273+
- 使用方式
274+
275+
```python
247276
#!/usr/bin/python3
248277
# -*- coding: utf-8 -*-
249278
'''
@@ -278,24 +307,28 @@ if __name__ == '__main__':
278307
except Exception as e:
279308
# 把 url 记录到日志文件中,未来进行手动分析,然后对url进行重新请求
280309
print(e)
281-
代码讲解
282-
安装 retrying 模块
310+
311+
```
312+
313+
- 代码讲解
314+
安装 ``retrying`` 模块
315+
316+
> retrying 模块可以通过装饰器模式对某个函数进行监控,如果该函数引发异常就会触发重试操作
283317
284-
retrying 模块可以通过装饰器模式对某个函数进行监控,如果该函数引发异常就会触发重试操作
318+
> pip install retrying
285319
286-
pip install retrying
287-
对需要重试的函数进行装饰器设置
320+
- 对需要重试的函数进行装饰器设置
288321

289-
通过 @retry(stop_max_attempt_number=重试次数) 参数设置重试次数
322+
> 通过 ``@retry(stop_max_attempt_number=重试次数)`` 参数设置重试次数
290323
324+
```python
291325
# 1. 导入模块
292326
from retrying import retry
293327
# 2. 装饰器设置重试函数
294328
@retry(stop_max_attempt_number=3)
295329
def exec_func():
296330
pass
297-
298-
331+
```
299332

300333

301334

0 commit comments

Comments
 (0)