安裝
pip install requests
方法
requests.get() 發起get請求調用 查詢
requests.post() 發起post請求調用 報錯
requests.put() 發起put請求調用 修改
requests.delete() 發起delete請求調用 刪除
requests.session() 獲取requests的session對象
requests.session().request() 也是發起請求,可以自動管理cookie
http://82.156.74.26:9088/pinter/doc
get請求
requests.get(url, params=params, headers=headers) 正常的get請求
post請求
requests.post(url, data=data, headers=headers ) post正常請求
requests.post(url, json=data, headers=headers ) post使用json格式數據
上傳下載文件
#文件下載
data = {"type":4, " path": path, " name": name, "token":token}
res = requests.get(url=down_url,params= data)
with open(file_name,"wb") as code:code.write(down_res.content)
# 上傳文件
file = {'file': open(r'D:\ak47.jpg', 'rb')}
requests.post(url=url, files=file) 上傳文件
cookie關聯
#獲取session請求
session = requests.session()
#登錄
session.request(url, meshod='post', data=data)
#登錄之后,訪問服務器的時候,請求中就自帶了cookie,其中就有了用戶信息,后端就知道是那個用戶了
session.request()
token關聯
headers={'testfan-token':token
}
# 在header中帶上token信息就行,token可以從登錄信息中獲取到
requests.get(url=url,headers=headers)
MD5使用
def get_md5(data):md5 = hashlib.md5()md5.update(data.encode('utf-8'))return md5.hexdigest()
其他
- json處理
pip install jsonpath
絕對路徑 $.store.book[]
相對路徑問題 $…book[]
2. 非json的可以用正則匹配
3. 有些請求里面需要在header中添加指定的數據格式才行如: 鏈接 https://blog.csdn.net/kxkltey/article/details/106683790