Akemi

Python request模块常用方法

2024/11/10

Request模块说明

需要手动安装
pip3 install requests

核心功能

1.发送HTTP请求,支持GET POST PUT DELETE HEADOPTIONS等方法
2.处理请求参数,支持传递URL参数等
3.处理相应
4.简化认证,内置基本HTTP认证,简化了身份验证的流程
5.可以通过会话对象,在多次请求之间保持cookie等信息
6.自动处理301重定向和GZIP压缩
7.支持超时和异常处理

常用方法

request.get 发送get请求
request.post 发送post请求
request.put 发送put请求
request.delete
request.head
request.options

一些常见参数

url :网址,或者是api接口
kwars其他参数:headers(自定义请求头),timeout,cookie

常用免费测试API的地址

http://httpbin.org,用于测试各种HTTP请求方法

https://jsonplaceholder.typeicode.com/posts,提供RESTful API接口,模拟CRUD操作

https://postman-echo.com/get,postman接口调试工具:适合做get请求,返回详细信息

http响应头

是一个字典对象,包含了服务器返回的所有响应头信息

包括:
Content-Type资源内容的类型,如html,txt,json
Content-Length响应体的长度
Last-Modify资源最后修改的时间
Cache-Control缓存指令

http响应头用来检查资源的存在性,获取资源信息

Request模块常用方法

get方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#coding=utf-8
import requests
url='http://www.baidu.com/s'
headers = {
'User-Agent':'Mozilla/5.0'
}
params = {
'wd':'python'
}
response = requests.get(url,params=params, headers=headers)
if response.status_code == 200:
print("请求成功")
print(response.text)
else:
print("请求失败")

post方法

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
27
28
29
30
31
32
33
34
35
36
#coding=utf-8
import requests

response=requests.post(
'http://httpbin.org/post',
json={'key':'value'},
headers={'Content-Type':'application/json'}
)
print(response.status_code)
print(response.text)
mesg=response.json()
print(mesg['origin'])

# 200
# {
# "args": {},
# "data": "{\"key\": \"value\"}",
# "files": {},
# "form": {},
# "headers": {
# "Accept": "*/*",
# "Accept-Encoding": "gzip, deflate, br",
# "Content-Length": "16",
# "Content-Type": "application/json",
# "Host": "httpbin.org",
# "User-Agent": "python-requests/2.31.0",
# "X-Amzn-Trace-Id": "Root=1-6730b043-46332a5704ec7e4e7804bd46"
# },
# "json": {
# "key": "value"
# },
# "origin": "60.190.187.166",
# "url": "http://httpbin.org/post"
# }
#
# 60.190.187.166

post方法——cookie参数

1
2
3
4
5
6
7
import requests

cookie={'session_id':'abc123', 'user':'wangsheng'}
response = requests.post('http://httpbin.org/post',cookies=cookie)
print(response.text)

# "Cookie": "session_id=abc123; user=wangsheng",

delete、head、option方法

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
#coding=utf-8
import requests

# delete方法
response = requests.delete('https://httpbin.org/delete')
if response.status_code == 204:
print('Delete successful')
else:
print(f'Delete failed,status_code is {response.status_code}')
# Delete failed,status_code is 200
# 因为这只是个测试接口,并不允许真的删除

# head方法
response = requests.head('https://httpbin.org/headers')
print(response.headers)
# {'Date': 'Sun, 10 Nov 2024 13:46:23 GMT', 'Content-Type': 'application/json', 'Content-Length': '229', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'}

# option方法
response = requests.options('https://httpbin.org/headers')
print(response.headers)
#'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS', 'Access-Control-Max-Age': '3600'}

# headers——打印allow允许的请求方法
print(response.headers['Allow'])
# OPTIONS, HEAD, GET

timeout参数连接超时、auth参数身份验证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#coding=utf-8
import requests

# timeout参数
try:
response = requests.get(
'http://jsonplaceholder.typicode.com/posts123',
timeout=0.01)
print(f"Request Success,status_code={response.status_code}")
except requests.Timeout as e:
print(f"Request Timeout:{e}")

# Request Timeout:HTTPConnectionPool(host='jsonplaceholder.typicode.com', port=80): Max retries exceeded with url: /posts123 (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x0000019126226010>, 'Connection to jsonplaceholder.typicode.com timed out. (connect timeout=0.01)')

# auth参数
response = requests.get('http://httpbin.org/basic-auth/user/pass',auth=('user','pass'))
if response.status_code == 200:
print("验证成功:\n",response.json())
else:
print("身份验证失败,状态码:",response.status_code)
# 验证成功:
# {'authenticated': True, 'user': 'user'}

# 身份验证失败,状态码: 401

fiie参数模拟文件上传、cookies参数发送带cookies的请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#coding=utf-8
import requests

# 模拟上传文件
files={'file':open('deployment.yaml','rb')}
response=requests.post('https://httpbin.org/post',files=files)
print('file uploaded successfully')

# 模拟带cookie的请求
response=requests.get('https://httpbin.org/cookies',cookies={'session_id':'wangsheng'})
print("Cookies sent successfully,status code:",response.status_code)
print("return Cookies:",response.json())

# Cookies sent successfully,status code: 200
# return Cookies: {'cookies': {'session_id': 'wangsheng'}}

allow_redirect参数禁用重定向

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#coding=utf-8
import requests
response = requests.get('http://httpbin.org/redirect/3',allow_redirects=False)
print("http status code:",response.status_code)
print('redirect url:',response.headers.get('Location'))
print(response.text)
# http status code: 302
# redirect url: /relative-redirect/2
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
# <title>Redirecting...</title>
# <h1>Redirecting...</h1>
# <p>You should be redirected automatically to target URL: <a href="/relative-redirect/2">/relative-redirect/2</a>. If not click the link.
# ↑无法正常获取了,因为禁用了自动重定向

# 查看历史重定向链接
response=requests.get('http://httpbin.org/redirect/3')
for r in response.history:
print('重定向历史链接:',r.url)

# 重定向历史链接: http://httpbin.org/redirect/3
# 重定向历史链接: http://httpbin.org/relative-redirect/2
# 重定向历史链接: http://httpbin.org/relative-redirect/1
CATALOG
  1. 1. Request模块说明
  2. 2. Request模块常用方法
    1. 2.1. get方法
    2. 2.2. post方法
    3. 2.3. post方法——cookie参数
    4. 2.4. delete、head、option方法
    5. 2.5. timeout参数连接超时、auth参数身份验证
    6. 2.6. fiie参数模拟文件上传、cookies参数发送带cookies的请求
    7. 2.7. allow_redirect参数禁用重定向