requests模块中通用的请求方法,即requests.request

import requests

 1、通用的request请求方法,但是需要多添加一个参数即请求方法,格式为requests.request("请求方法","请求信息")

# res = requests.get('https://httpbin.org/get')
# res = requests.request('GET', 'https://httpbin.org/get')
#
# res = requests.post('https://httpbin.org/post', data={'a': 1})
# res = requests.request('POST', 'https://httpbin.org/post', data={'a': 1})

2、

   ①可以将请求数据单独提取出来,然后传给共用的requests请求方法,通过for循环,来完成不同请求方法的接口,
   ②字典可用dict(a=b)或者{"a":"b"}两种方式来表达;
   ③解包使用**

import requests
# res = requests.request(
#     method='post',   # 也可以只写'post',
#     url='https://httpbin.org/post',  # 也可以只写'https://httpbin.org/post',
#     headers={},
#     data={'name': '临渊', 'password': '123456'}
# )

req1 = dict(
    method='post',   # 也可以只写'post',
    url='https://httpbin.org/post',  # 也可以只写'https://httpbin.org/post',
    headers={},
    data={'name': '临渊', 'password': '123456'}
)

req2 = dict(
    method='get',   # 也可以只写'get',
    url='https://httpbin.org/get',  # 也可以只写'https://httpbin.org/post',
    headers={},
)

req_list = [req1, req2]
# req1 = {
#     'method': 'post',
# }

for req in req_list:
    res = requests.request(**req)    #需要通过**来获取到原始的字典格式数据
    print(res.text)



posted @ 2020-11-05 12:57  听MM的话  阅读(1389)  评论(0编辑  收藏  举报