标签:open method header author 响应 lse 抛出异常 com too
pip install requests
# -*- coding: utf-8 -*- # @Time : 2021/5/11 23:25 # @Author : chinablue # @File : tmp0512.py import requests req_data = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘} resp = requests.get("http://httpbin.org/get", params=req_data) print(f"请求地址:{resp.url}") print(f"请求方式:{resp.request.method}") print(f"请求类型:{resp.request.headers.get(‘Content-Type‘)}")
执行结果
请求地址:http://httpbin.org/get?key1=value1&key2=value2 请求方式:GET 请求类型:None #默认的Content-Type为application/x-www-form-urlencoded
# -*- coding: utf-8 -*- # @Time : 2021/5/11 23:25 # @Author : chinablue # @File : tmp0512.py import requests req_data = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘} resp = requests.post("http://httpbin.org/post", data=req_data) print(f"请求地址:{resp.url}") print(f"请求方式:{resp.request.method}") print(f"请求类型:{resp.request.headers.get(‘Content-Type‘)}") print(f"请求数据:{resp.request.body}")
执行结果
请求地址:http://httpbin.org/post 请求方式:POST 请求类型:application/x-www-form-urlencoded 请求数据:key1=value1&key2=value2
# -*- coding: utf-8 -*- # @Time : 2021/5/11 23:25 # @Author : chinablue # @File : tmp0512.py import requests req_data = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘} resp = requests.post("http://httpbin.org/post", json=req_data) # resp = requests.put("http://httpbin.org/put", json=req_data) # resp = requests.delete("http://httpbin.org/delete", json=req_data) print(f"请求地址:{resp.url}") print(f"请求方式:{resp.request.method}") print(f"请求类型:{resp.request.headers.get(‘Content-Type‘)}") print(f"请求数据:{resp.request.body}")
执行结果
请求地址:http://httpbin.org/post 请求方式:POST 请求类型:application/json 请求数据:b‘{"key1": "value1", "key2": "value2"}‘
# -*- coding: utf-8 -*- # @Time : 2021/5/11 23:25 # @Author : chinablue # @File : tmp0512.py import requests req_data = {‘key1‘: ‘value1‘, ‘key2‘: ‘value2‘} resp = requests.post("http://httpbin.org/post", files=req_data) print(f"请求地址:{resp.url}") print(f"请求方式:{resp.request.method}") print(f"请求类型:{resp.request.headers.get(‘Content-Type‘)}") print(f"请求数据:{resp.request.body}")
执行结果
请求地址:http://httpbin.org/post 请求方式:POST 请求类型:multipart/form-data; boundary=a59060b098198d3900189d1e51504c4b 请求数据:b‘--a59060b098198d3900189d1e51504c4b\r\nContent-Disposition: form-data; name="key1"; filename="key1"\r\n\r\nvalue1\r\n--a59060b098198d3900189d1e51504c4b\r\nContent-Disposition: form-data; name="key2"; filename="key2"\r\n\r\nvalue2\r\n--a59060b098198d3900189d1e51504c4b--\r\n‘
说明事项:
1. files参数中如果有文件数据,其格式如下:
files = { "file": ("1.png", open("1.png", "rb"), "images/png") }
2. 如果上传的文件比较大,建议使用requests-toolbelt库来辅助
1. requests库支持所有的HTTP请求,如get,post,put,delete等
2. 发送请求时,有4个参数可以传入请求数据:params(参数在url上),data,json,files
# -*- coding: utf-8 -*- # @Time : 2021/5/11 23:25 # @Author : chinablue # @File : tmp0512.py import requests resp = requests.get("http://httpbin.org/get") print(f"响应状态码: {resp.status_code}") print(f"响应头信息: {resp.headers}") print(f"响应cookies信息: {resp.cookies}") print(f"获取PreparedRequest对象:{resp.request}") print(f"响应内容(str类型): {resp.text}") print(f"响应内容(dict类型): {resp.json()}") #如果响应类型不是json,则抛出异常 print(f"响应内容(bytes类型): {resp.content}")
说明事项:
1. resp.cookies返回的是一个RequestsCookieJar对象,若将其转为字典对象:
# -*- coding: utf-8 -*- # @Time : 2021/5/11 23:25 # @Author : chinablue # @File : tmp0512.py import requests resp = requests.get("https://www.baidu.com/img/flexible/logo/pc/result.png") print(f"响应cookies信息(字典类型): {requests.utils.dict_from_cookiejar(resp.cookies)}") print(f"响应cookies信息(字典类型): {resp.cookies.get_dict()}") print(f"响应cookies信息(列表类型): {resp.cookies.items()}")
2. resp.request返回的是一个PreparedRequest对象,可以从该对象中获取到相关请求信息
print(f"请求地址:{resp.request.url}") print(f"请求方法:{resp.request.method}") print(f"请求头信息:{resp.request.headers}")
3. resp.content返回的是二进制数据,一般用于获取图片或文件数据
# -*- coding: utf-8 -*- # @Time : 2021/5/11 23:25 # @Author : chinablue # @File : tmp0512.py import requests resp = requests.get("https://www.baidu.com/img/flexible/logo/pc/result.png") if resp.status_code == requests.codes.ok: with open("baidu.png", "wb") as f: f.write(resp.content) else: raise Exception(f"请求失败,当前http响应码: {resp.status_code}")
python3接口自动化--requests库的使用方法(一)
标签:open method header author 响应 lse 抛出异常 com too
原文地址:https://www.cnblogs.com/reconova-56/p/14761771.html