詳解Python 重學requests發(fā)起請求的基本方式
更新時間:2020年02月07日 11:37:38 作者:haeasringnar
這篇文章主要介紹了詳解Python 重學requests發(fā)起請求的基本方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
安裝相關模塊
pip install requests requests-toolbelt
代碼實例
import requests
import json
from PIL import Image
from io import BytesIO
from requests_toolbelt import MultipartEncoder
'''
使用 requests 請求返回的 response 注意事項
response.text 獲得響應結果的字符串類型
response.content 獲得響應結果的bytes(二進制數(shù)據(jù)流類型,可用來處理返回的二進制文件流) 如果是圖片的話可以使用 Image.open(BytesIO(r.content)).show() 打開查看
response.status_code 獲得響應結果的狀態(tài)碼
response.headers 獲得響應結果的請求頭
response.encoding 獲得響應結果的編碼
response.url 獲得請求的url
response.json() 將獲得響應結果轉換成 json.loads(str) 后的結果,在python中得到字典類型
'''
def get_request(url, params, headers=None):
'''
發(fā)起GET請求
:url 請求的地址 字符串類型
:params 請求的參數(shù) 字典類型
:headers 定義請求頭 字典類型
'''
return requests.get(url=url, params=params, headers=headers)
def post_www_form_request(url, www_form, headers=None):
'''
發(fā)起POST請求 發(fā)送x-www-form-urlencoded請求體
:url 請求的地址 字符串類型
:www_form x-www-form-urlencoded請求體 字典類型
:headers 定義請求頭 字典類型
'''
return requests.post(url=url, data=www_form, headers=headers)
def post_form_data_request(url, form_data, headers=None):
'''
發(fā)起POST請求 發(fā)送form-data請求體
:url 請求的地址 字符串類型
:form_data form-data請求體 字典類型
:headers 定義請求頭 字典類型
'''
default_headers = {'Content-Type': 'multipart/form-data'}
if headers:
default_headers.update(headers)
m = MultipartEncoder(fields=form_data)
default_headers['Content-Type'] = m.content_type
print(default_headers)
return requests.post(url=url, data=m, headers=default_headers)
def post_json_data_request(url, json_data, headers=None):
'''
發(fā)起POST請求 發(fā)送json請求體
:url 請求的地址 字符串類型
:json_data json類型請求體 字典類型
:headers 定義請求頭 字典類型
'''
# 方式一
# default_headers = {'Content-Type': 'application/json'}
# if headers:
# default_headers.update(headers)
# return requests.post(url=url, data=json.dumps(json_data), headers=default_headers)
# 方式二
return requests.post(url=url, json=json_data, headers=headers)
def post_files_request(url, files, headers=None):
'''
發(fā)起POST請求 請求體為文件
:url 請求的地址 字符串類型
:files 文件類型請求體 文件類型
:headers 定義請求頭 字典類型
'''
# 攜帶請求頭
default_headers = {'Authorization': 'bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZXhwIjoxMDIxNTk5MjgwMX0.GFs_smaKQ55taYgctbDzw2ooOdKNuy-HqobHXB2nE1o'}
if headers:
default_headers.update(headers)
return requests.post(url=url, files=files, headers=default_headers)
if __name__ == '__main__':
# 測試GET請求
# print(get_request('http://127.0.0.1:9000/wechat/good/', {'page': 1, 'page_size': 2}).json())
# print(post_www_form_request('http://127.0.0.1:9000/mobilelogin/', {'mobile': '17316280277', 'code': '1234'}).json())
# (('mobile', '17316280277'), ('code', '1234'))
# print(post_form_data_request('http://127.0.0.1:9000/mobilelogin/', {'mobile': '17316280277', 'code': '1234'}).json())
# print(post_json_data_request('http://127.0.0.1:9000/mobilelogin/', {'mobile': '17316280277', 'code': '1234'}).json())
print(post_files_request('http://127.0.0.1:9000/uploadfile/', {'file': open('img1.png', 'rb'), 'file1': open('1.xls', 'rb')}).json())
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
使用python解析MDX詞典數(shù)據(jù)并保存為Excel文件
MDX(Mobile Dictionary eXchange)是一種常見的詞典文件格式,通常用于在移動設備和電腦之間共享辭典數(shù)據(jù),本文深入探討了從MDX詞典數(shù)據(jù)提取、處理到最終保存為Excel文件的全過程,需要的朋友可以參考下2023-12-12
Python中私有屬性“_“下劃線和“__“雙下劃線區(qū)別
本文主要介紹了Python中私有屬性“_“下劃線和“__“雙下劃線區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03
利用python實現(xiàn)簡易版的貪吃蛇游戲(面向python小白)
這篇文章主要給大家介紹了關于如何利用python實現(xiàn)簡易版的貪吃蛇游戲的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-12-12
利用python抓取HTML頁面數(shù)據(jù)并作可視化數(shù)據(jù)分析
這篇文章主要為大家詳細介紹了如何利用python抓取HTML頁面數(shù)據(jù)并作可視化數(shù)據(jù)分析,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下2025-04-04

