requests —— 基本用法

请求类型

requests可以用一句话来处理请求的类型。

1
2
3
4
5
r = requests.get('http://httpbin.org/get')
r = requests.post('http://httpbin.org/post')
r = requests.delete('http://httpbin.org/delete')
r = requests.head('http://httpbin.org/get')
r = requests.options('http://httpbin.org/get')

GET请求

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import requests

url = 'http://httpbin.org/get'
r = requests.get(url)
print(type(r)) # <class 'requests.models.Response'>
print(r.text)
print(type(r.text)) # <class 'str'>
print(r.content)
print(type(r.content)) # <class 'bytes'>
print(r.content.decode())
print(r.status_code) # 状态码
print(r.headers) # 响应头
print(type(r.headers))
print(r.cookies)
print(type(r.cookies)) # <class 'requests.cookies.RequestsCookieJar'>
print(r.url)
print(r.history) # 得到请求历史

GET请求添加参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import requests

data = {
'name':'David',
'age':22
}
response = requests.get('http://httpbin.org/get',params=data)
print(response.text) # 返回的是'str'类型,并且是JSON格式
print(response.json())
print(type(response.json())) # 返回的是'dict'类型
'''
{
"args": {
"age": "22",
"name": "David"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"origin": "61.171.88.179, 61.171.88.179",
"url": "https://httpbin.org/get?name=David&age=22"
}
'''

添加请求头

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import requests

url = 'https://www.zhihu.com/explore'

# 直接请求
response = requests.get(url)
print(response.status_code) # 400,不添加请求头的情况下,知乎会禁止你访问

# 添加请求头
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
}
response = requests.get(url,headers=headers) # 添加请求头
print(response.status_code) # 200

抓取二进制数据

抓取图片、音频、视频等文件是二进制数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import requests

url = 'https://static.zhihu.com/static/favicon.ico'

response = requests.get(url)
print(response.text)
# 乱码
# If Response.encoding is None, encoding will be guessed using
# 编码格式没有设置的话,靠猜
print(response.text.encode('utf-8')) # 就与content返回的数据一样
print(response.content) # 二进制数据 bytes类型 无论怎么样最好都使用content

with open('zhihu.ico','wb') as f:
f.write(response.content)

POST请求

基本实例

1
2
3
4
5
6
7
8
9
10
11
12
13
import requests

url = 'http://httpbin.org/post'
data = {
'name':'David',
'age':'22'
}

response = requests.post(url,data=data)
print(response.content)
print(response.content.decode('utf-8'))

# 返回结果中的form部分就是我们提交的数据,这就证明了post请求成功了