一、Requests簡介
Python的Requests庫是一個功能強大且簡潔的庫,提供了簡單易用的接口來處理HTTP請求。
二、Requests的使用步驟?
2.1 安裝Requests庫
在終端命令行,使用pip命令進行安裝,
pip install requests
2.2 Requests庫常用方法
1.發送get請求(通過params傳參)
requests.get('http://www.example.com')
2.發送post請求(通過data,json傳參)
requests.post('http://www.example.com', data={'key': 'value'})
2.3 請求頭格式
請求頭:
1.請求正文格式:?multipart/form-daa? ?用于文件上傳
2.請求正文格式:application/x-wwnw-form-urlencoded? 鍵值對,以表單的方式傳參
3.請求正文格式:raW
- ????????Content-Type:application/json
- ????????Content-Type:text/plain
- ????????Content-Type:application/javascnpt
- ????????Content-Type:text/html
- ????????Content-Type:application/xml
4.請求正文格式:binary
Content-Type:application/octet-stream? 二進制流的數據
2.4 請求方式
requests.get()?get 傳參,一般為params=Nonerequests.post()
requests.put()
requests.delete()post,get,patch傳參一般為data=Nonerequests.request()參數詳解method: 請求方式url:請求路徑params=None get方式傳參data=None post方式,put,patch傳參headers=None 請求頭cookies=None 請求的cookie信息files=None 文件上傳json=None post傳參 requests.session() 發送任意請求
2.5 Response響應對象?
print(res.text)響應的字符串格式的據
print(res.content)響應的bytes類型格式的數據
print(res.json())響應的字典數據格式
print(res.status_code)響應的狀態碼
print(res.reason)響應的狀態信息
print(res.cookies)響應的cookie信息
print(res.headers)響應頭
print(res.request.headers)請求頭