詳解Python 重學(xué)requests發(fā)起請(qǐng)求的基本方式
安裝相關(guān)模塊
pip install requests requests-toolbelt
代碼實(shí)例
import requests import json from PIL import Image from io import BytesIO from requests_toolbelt import MultipartEncoder ''' 使用 requests 請(qǐng)求返回的 response 注意事項(xiàng) response.text 獲得響應(yīng)結(jié)果的字符串類型 response.content 獲得響應(yīng)結(jié)果的bytes(二進(jìn)制數(shù)據(jù)流類型,可用來處理返回的二進(jìn)制文件流) 如果是圖片的話可以使用 Image.open(BytesIO(r.content)).show() 打開查看 response.status_code 獲得響應(yīng)結(jié)果的狀態(tài)碼 response.headers 獲得響應(yīng)結(jié)果的請(qǐng)求頭 response.encoding 獲得響應(yīng)結(jié)果的編碼 response.url 獲得請(qǐng)求的url response.json() 將獲得響應(yīng)結(jié)果轉(zhuǎn)換成 json.loads(str) 后的結(jié)果,在python中得到字典類型 ''' def get_request(url, params, headers=None): ''' 發(fā)起GET請(qǐng)求 :url 請(qǐng)求的地址 字符串類型 :params 請(qǐng)求的參數(shù) 字典類型 :headers 定義請(qǐng)求頭 字典類型 ''' return requests.get(url=url, params=params, headers=headers) def post_www_form_request(url, www_form, headers=None): ''' 發(fā)起POST請(qǐng)求 發(fā)送x-www-form-urlencoded請(qǐng)求體 :url 請(qǐng)求的地址 字符串類型 :www_form x-www-form-urlencoded請(qǐng)求體 字典類型 :headers 定義請(qǐng)求頭 字典類型 ''' return requests.post(url=url, data=www_form, headers=headers) def post_form_data_request(url, form_data, headers=None): ''' 發(fā)起POST請(qǐng)求 發(fā)送form-data請(qǐng)求體 :url 請(qǐng)求的地址 字符串類型 :form_data form-data請(qǐng)求體 字典類型 :headers 定義請(qǐng)求頭 字典類型 ''' 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請(qǐng)求 發(fā)送json請(qǐng)求體 :url 請(qǐng)求的地址 字符串類型 :json_data json類型請(qǐng)求體 字典類型 :headers 定義請(qǐng)求頭 字典類型 ''' # 方式一 # 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請(qǐng)求 請(qǐng)求體為文件 :url 請(qǐng)求的地址 字符串類型 :files 文件類型請(qǐng)求體 文件類型 :headers 定義請(qǐng)求頭 字典類型 ''' # 攜帶請(qǐng)求頭 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__': # 測(cè)試GET請(qǐng)求 # 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())
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用Python爬蟲庫requests發(fā)送表單數(shù)據(jù)和JSON數(shù)據(jù)
- Python爬蟲庫requests獲取響應(yīng)內(nèi)容、響應(yīng)狀態(tài)碼、響應(yīng)頭
- python用requests實(shí)現(xiàn)http請(qǐng)求代碼實(shí)例
- Python3離線安裝Requests模塊問題
- Python3+Requests+Excel完整接口自動(dòng)化測(cè)試框架的實(shí)現(xiàn)
- Python使用grequests(gevent+requests)并發(fā)發(fā)送請(qǐng)求過程解析
- python requests證書問題解決
- Python requests獲取網(wǎng)頁常用方法解析
相關(guān)文章
Django 開發(fā)環(huán)境與生產(chǎn)環(huán)境的區(qū)分詳解
這篇文章主要介紹了Django 開發(fā)環(huán)境與生產(chǎn)環(huán)境的區(qū)分詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07解決Django中調(diào)用keras的模型出現(xiàn)的問題
今天小編就為大家分享一篇解決Django中調(diào)用keras的模型出現(xiàn)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08Tensorflow 定義變量,函數(shù),數(shù)值計(jì)算等名字的更新方式
今天小編就為大家分享一篇Tensorflow 定義變量,函數(shù),數(shù)值計(jì)算等名字的更新方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02基于django和dropzone.js實(shí)現(xiàn)上傳文件
這篇文章主要介紹了基于django和dropzone.js實(shí)現(xiàn)上傳文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Python使用PIL庫實(shí)現(xiàn)驗(yàn)證碼圖片的方法
這篇文章主要介紹了Python使用PIL庫實(shí)現(xiàn)驗(yàn)證碼圖片的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python基于PIL庫生成驗(yàn)證碼圖片的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-03-03Python flask框架請(qǐng)求體數(shù)據(jù)、文件上傳、請(qǐng)求頭信息獲取方式詳解
這篇文章主要介紹了Python flask框架請(qǐng)求體數(shù)據(jù)、文件上傳、請(qǐng)求頭信息獲取方式詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03Flask框架學(xué)習(xí)筆記之路由和反向路由詳解【圖文與實(shí)例】
這篇文章主要介紹了Flask框架學(xué)習(xí)筆記之路由和反向路由,結(jié)合圖文與實(shí)例形式詳細(xì)分析了flask框架中路由與反向路由相關(guān)概念、原理、用法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-08-08將python代碼打包成.exe文件直接運(yùn)行的具體步驟
小編最近收到了一個(gè)小伙伴的問題,就是那么多有趣的代碼,怎么發(fā)給別人,讓沒有python環(huán)境的小伙伴也可以使用呢,本文小編將帶著大家探索如何將自己的python代碼打包成.exe可執(zhí)行文件,一起來看看吧2024-02-02