Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0167c6e

Browse files
Create kc45.md
1 parent 30246f8 commit 0167c6e

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

‎kc/kc45.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# 4-5 Python标准库
2+
## 1.python标准库urllib
3+
urllib库主要用于操作网页 URL,处理网络请求,它是python的标准库,不需要单独去安装,它主要包含四大功能模块
4+
5+
- urllib.request 发送GET或者POST请求
6+
- urllib.error 异常处理模块(在实际项目中,可以根据捕获的异常信息明白程序代码的故障)
7+
- urllib.parse 解析模块(网络传输中经常会把数据进行编码处理)
8+
- urllib.robotparser robot.txt文件解析模块
9+
10+
## 发起一个请求
11+
先看一下,发起基础请求的方法urlopen的函数原型
12+
13+
urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None,context=None)
14+
15+
- url 请求的地址
16+
- data 请求地址的附带参数,可以不传 参数要通过bytes(urllib.parse.urlencode(params), encoding='utf8') 进行字节转换,编码格
17+
式以后台的请求约定而确定
18+
- timeout 请求的超时时间,主要用于,当网络不好时,能最快的返回数据,而不是一直在等待返回
19+
- cafile、capath、context 暂不做学习
20+
### 代码区 1
21+
```python
22+
1 # 这里用到了as关键字,as相当于,指定别名的意思,主要是为了简写
23+
2 import urllib.request as rq
24+
3
25+
4 # 定义一个请求地址,示例以新浪主页
26+
5 url = 'https://www.sina.com.cn/'
27+
6
28+
7 # 用到了urlopen进行发送请求
29+
8 res = rq.urlopen(url)
30+
9
31+
10 # 读取请求到的返回值 就相当于打开了一些新浪主页,会返回该页面的所有元素
32+
11 html = res.read()
33+
12
34+
13 # 将返回的数据进行编码转换 编码转换主要是为了解决汉字乱码的事儿,如果是英文网站,可以不用
35+
14 # 如果打印出来看着不方便 ,可以保存到文件中看一下
36+
15 print(html.decode('utf-8'))
37+
16
38+
```
39+
![4](https://user-images.githubusercontent.com/103555341/163546933-bee710b5-943e-454e-b00d-922d2b897614.jpg)
40+
### 代码区 2
41+
```python
42+
1 # 练习题 试着,请求一下QQ的主页,并把请求到的结果,保存到文件中
43+
2
44+
```
45+
![4](https://user-images.githubusercontent.com/103555341/163546933-bee710b5-943e-454e-b00d-922d2b897614.jpg)
46+
47+
## 设置一个超时
48+
在请求当中,可能会因为网络原因或者服务器原因,造成数据无法返回,这个时候,如果读取请求的返回数据,就会造成卡死的情况,这时就可以
49+
50+
用到超时参数 timeout
51+
### 代码区 3
52+
```python
53+
1 import urllib.request as rq
54+
2
55+
3 # 定义一个请求地址,示例以新浪主页
56+
4 url = 'https://www.sina.com.cn/'
57+
5
58+
6 # 用到了urlopen进行发送请求 ,并指定超时时间为1秒 ,如果超时,会自动舍弃请求
59+
7 res = rq.urlopen(url, timeout=1)
60+
8
61+
9 print(res.read().decode('utf-8'))
62+
```
63+
![4](https://user-images.githubusercontent.com/103555341/163546933-bee710b5-943e-454e-b00d-922d2b897614.jpg)
64+
65+
## 2.python库-Requests
66+
Python中的标准库urllib模块,已经可以实现开发当中的请求功能,但相对来说,urllib的库还不是很简洁,使用起来相对比较复杂、别扭
67+
68+
Requests库的应用更符合Python的追求简洁、优美、轻快,真正让HTTP服务开发者
69+
70+
Requests对urllib模块进行升级封装,使用起来更方便、快捷
71+
72+
Requests模块不是Python的标准库,因此使用之前需要先进行pip安装
73+
### 使用Requests发送一个Get请求
74+
上面的urllib模块,验证发送了一个请求,读到请求结果,需要urlopen然后read,Requests只有一步就可以搞定
75+
76+
系统已集成Requests模块,不需要再安装
77+
### 代码区 4
78+
```python
79+
1 # 这里用到了as关键字,as相当于,指定别名的意思,主要是为了简写
80+
2 import requests as rq
81+
3
82+
4 # 代帮网学习网提供的供学习的一个GET请求接口,该接口,会返回你发送的请求数据
83+
5 response = rq.get('http://muser.mp58.net:8101/dmbget')
84+
6 print(response.text)
85+
7
86+
```
87+
![4](https://user-images.githubusercontent.com/103555341/163546933-bee710b5-943e-454e-b00d-922d2b897614.jpg)
88+
```python
89+
{"code": 200, "params": {}, "msg": ""}
90+
```
91+
### 使用Requests发送一个post请求
92+
### 代码区 5
93+
```python
94+
1 # 这里用到了as关键字,as相当于,指定别名的意思,主要是为了简写
95+
2 import requests as rq
96+
3
97+
4 # 代帮网学习网提供的供学习的一个POST请求接口,该接口,会返回你发送的请求数据
98+
5 url = 'http://muser.mp58.net:8101/dmbpost'
99+
6
100+
7 params = {
101+
8 'username': 'dmbpost'
102+
9 }
103+
10 response = rq.post(url, params=params)
104+
11 print(response.text)
105+
12
106+
```
107+
![4](https://user-images.githubusercontent.com/103555341/163546933-bee710b5-943e-454e-b00d-922d2b897614.jpg)
108+
```python
109+
{"code": 200, "params": {"username": "dmbpost"}, "msg": ""}
110+
```
111+
### 代码区 6
112+
```python
113+
1 # 练习题 试下发送一个自已的请求
114+
2
115+
```
116+
![4](https://user-images.githubusercontent.com/103555341/163546933-bee710b5-943e-454e-b00d-922d2b897614.jpg)
117+
118+
119+
120+
121+
122+
123+
124+
125+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /