Requests
Requests是用python语言基于urllib编写的,是Python实现的简单易用的HTTP库,可以用它来完成大部分HTTP协议行为,例如: HEAD、GET、POST、PUT、PATCH、DELETE请求等。Requests它会比urllib更加方便,可以节约我们大量的工作。
安装方法
默认安装好python之后,是没有安装requests模块的,需要单独通过pip安装
1、打开终端
2、pip install requests
Requests库的常用方法
方法名 | 说明 | 使用方法 |
---|---|---|
get | 获取数据 | requests.get() |
post | 提交数据 | requests.post() |
put | 更新数据 | requests.put() |
delete | 删除数据 | requests.delete() |
response对象的属性
status_code:响应状态码
url:响应的地址
text:响应文本内容
json():响应的json对象,就是把 json数据转成了Python中的字典或者列表
headers:响应头
cookies:获取响应的cookie信息
示例:
import requests
def weather():
url = "https://api.apiopen.top/getSingleJoke?sid=28654780"
# 获取请求数据
response = requests.get(url)
# 指定字符编码
response.encoding = "utf-8"
return response
if __name__ == '__main__':
resp = weather()
print("响应状态码:", resp.status_code)
print("响应的地址:", resp.url)
print("响应头:", resp.headers)
print("响应文本内容:", resp.text)
print("响应的cookie信息:", resp.cookies)
data = resp.json()
print("响应的json数据:", data, type(data))
# 获取响应的指定数据
result = data["result"]
print(result.get("text"))
GET请求
- 无查询参数
- 有查询参数
- 路径参数
示例:
import requests
# 使用get方式查询 - 无参查询
def get_departments():
url = "http://127.0.0.1:8000/api/departments/"
response = requests.get(url)
return response.json()
# 使用get方式查询 - 有参查询
def get_departments_by_query_params(name):
url = "http://127.0.0.1:8000/api/departments/"
params = {
"dep_name": {name}
}
response = requests.get(url, params=params)
return response.json()
# 使用get方式查询 - 路径参数查询
def get_departments_by_id(dep_id):
url = f"http://127.0.0.1:8000/api/departments/{dep_id}"
response = requests.get(url)
return response.json()
if __name__ == '__main__':
result = get_departments()
print(result)
result = get_departments_by_query_params("Python学院")
print(result)
result = get_departments_by_id("T03")
print(result)
POST请求
- 提交json数据
- 提交表单数据
示例:
import requests
# 使用post方式提交 - json数据
def add_department():
url = "http://127.0.0.1:8000/api/departments/"
json = {
"data": [
{
"dep_id": "T1001",
"dep_name": "Test学院",
"master_name": "Test-Master",
"slogan": "Here is Slogan"
}
]
}
response = requests.post(url, json=json)
return response.json()
# 使用post方式提交 - 表单数据
def add_department2():
url = "http://1.117.181.153:8085/admin_login/"
data = {
"username": "liyang",
"password": "admin123"
}
response = requests.post(url, data=data)
return response.json()
if __name__ == '__main__':
result = add_department()
print(result)
result = add_department2()
print(result)
PUT请求
示例:
import requests
# 使用put方式通过json方式修改数据
def update_department(dep_id, dep_name, master_name):
url = f"http://127.0.0.1:8000/api/departments/{dep_id}/"
json = {
"data": [
{
"dep_id": dep_id,
"dep_name": dep_name,
"master_name": master_name,
"slogan": "Here is Slogan"
}
]
}
response = requests.put(url, json=json)
return response.json()
if __name__ == '__main__':
result = update_department("T1023", "张三丰", "娃哈哈")
print(result)
DELETE请求
示例:
import requests
# 使用delete请求删除指定json数据
def remove_department(dep_id):
url = f"http://127.0.0.1:8000/api/departments/{dep_id}/"
response = requests.delete(url)
return response.ok
# 使用delete请求删除多个json数据
def remove_departments(dep_id_list):
# url = f"http://127.0.0.1:8000/api/departments/?$dep_id_list={dep_id_list}"
url = "http://127.0.0.1:8000/api/departments/"
params = {
"$dep_id_list": dep_id_list
}
response = requests.delete(url, params=params)
return response.ok
if __name__ == '__main__':
result = remove_department("T10234")
print("删除成功" if result.__eq__(True) else "删除失败")
result = remove_departments("T222,T333,333")
print("删除成功" if result.__eq__(True) else "删除失败")