1、requests是第三方库,需要自行安装:pip install requests
2、使用requests库,需要导入requests模块:import requests
import requests
# get请求
requests.get(url="", params="", headers="")
# post请求
requests.post(url="", data="表单数据数据", json="json格式数据", headers="")
# delete请求
requests.delete(url="", headers="")
# put请求
requests.put(url="", data="")
使用fiddler代理的时候,一定要把fiddler打开,不然会请求失败
# 使用fiddler代理
fiddler_proxies = {
"http": "http://127.0.0.1:8888",
"https": "http://127.0.0.1:8888"
}
# 加上proxies关键字参数
resp = requests.post(url, proxies=fiddler_proxies)
import requests
# 返回值类型:
# 返回值:
response = requests.get(url="https://www.baidu.com")
# 获取请求头信息:https://www.baidu.com/
request_url = response.request.url
# 获取请求体信息:NULL
request_body = response.request.body
# 获取请求头信息
# 请求头信息:{'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
request_headers = response.request.headers
# 获取响应头信息
response_headers = response.headers
# 获取响应码:200
response_status_code = response.status_code
# requests 库响应消息体四种格式
# 文本响应内容
# 获取html页面时使用
# 返回字符串类型数据
response_text = response.text
# json解码响应内容
# 此方法只适用于响应体类型是:json格式的,否则会报错
# 返回字典类型数据
response_body_json = response.json()
# 字节响应内容
# 下载图片或者文件时使用
# 返回字节类型数据
response_body_content = response.content
# 原始响应内容
# 返回原始格式
response_body_raw = response.raw
参数 |
get |
post |
put |
delete |
---|---|---|---|---|
url |
有 |
有 |
有 |
有 |
data |
否 |
有 |
有 |
有 |
json |
否 |
有 |
否 |
否 |
headers |
有 |
有 |
有 |
有 |
cookies |
有 |
有 |
有 |
有 |
params |
有 |
否 |
否 |
否 |
params参数:以查询字符串的形式把参数拼接到url
传参数据类型:
def get(url, params=None, **kwargs):
r"""Sends a GET request.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response ` object
:rtype: requests.Response
"""
kwargs.setdefault('allow_redirects', True)
return request('get', url, params=params, **kwargs)
def post(url, data=None, json=None, **kwargs):
r"""Sends a POST request.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response ` object
:rtype: requests.Response
"""
return request('post', url, data=data, json=json, **kwargs)
1、content-type:请求体为空
params参数:以查询字符串的形式把参数拼接到url
传参数据类型:
def baidu(inData):
url = "https://www.baidu.com"
payload = inData
requests.post(url, params=payload, proxies=fiddler_proxies, verify=False)
# 方式一:参数是字符串类型
inData = "username=xxx&password=yyy"
baidu(inData)
# 方式二:参数是字典类型
inData = {
"username": "xxx",
"password": "yyy"
}
baidu(inData)
2、Content-Type: application/x-www-form-urlencoded 请求体为表单格式
data参数:传递请求体数据,请求体默认为表单格式
传参数据类型:
# 方式一
payload={
username:"张三",
password:"123456"
}
# 方式二
payload="username=张三&password=123456"
# data默认请求体类型是:application/x-www-form-urlencoded
requests.post(url=url,data=payload)
3、Content-Type:application/json 请求体为json格式
json参数:传递请求体数据,请求体默认是json格式
传参数据类型:
4、Content-Type: multipart/form-data 请求体为文件格式
import requests
HOST="http://127.0.0.1:80"
fileUpload_path = "/user/doUpload"
fileUpload_data = {
"file": ("窗口.png", open("../../static/images/窗口.png", "rb"), "image/png")
}
class FileUpload:
def __init__(self, path, indata):
self.url = f'{HOST}{path}'
self.headers = {
"Content-Type": "multipart/form-data",
}
self.payload = indata
self.response = requests.post(self.url, files=self.payload)
def get_response_json(self):
return self.response.json()
fileUpload = FileUpload(fileUpload_path, fileUpload_data)
print(fileUpload.get_response_json())
1、 解决https协议请求异常:requests-SSL: CERTIFICATE_VERIFY_FAILED]
发送https请求时把验证ssl证书关掉
(1)请求参数verify=False
(2)去除警告:requests.packages.urllib3.disable_warnings() # 忽略警告
import requests
requests.packages.urllib3.disable_warnings() # 忽略警告
url = "https://www.baidu.com"
# requests.post(url=url)
requests.get(url=url, verify=False)