目錄
- 一、什么是requests,怎么安裝
- 二、requests 模塊的使用
- 2.1 get請求(最基本的請求)
- 2.2 post請求 form-data 格式的
- 2.3 post 請求上傳文件和別的參數(shù)
- 2.4 post 請求 json 形式的(常用)
- 三、總結(jié)
大凡人世間的痛苦,多是因放不下有時候我常想,痛苦,該是時光刮給生命的一場颶風(fēng)吧生活,本就是以這樣一種特別的方式,掀起遮蓋的一切,讓你看到人生的真相。
一、什么是requests,怎么安裝
requests 是python 的一個模擬發(fā)送請求的庫, 基本上調(diào)用別人接口的時候,這個是現(xiàn)在主流
安裝的話 直接pip就行了
二、requests 模塊的使用
我們通常進(jìn)行請求之前都會先去postman上面去模擬一遍,看是否請求的通,然后再進(jìn)行接口模擬,下面的介紹都是一個postman的圖 之后就是用請求的代碼這樣看起來比較直觀一點
2.1 get請求(最基本的請求)

直接進(jìn)行一個get請求的代碼是這樣的
# 導(dǎo)入模塊
import requests
# 定義請求地址
url = 'http://www.baidu.com/'
# 定義自定義請求頭
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"
}
# 定義 GET 請求參數(shù)
params = {
"kw":"dzw"
}
# 使用 GET 請求參數(shù)發(fā)送請求
response = requests.get(url,headers=headers,params=params)
# 獲取響應(yīng)的 html 內(nèi)容
html = response.text
2.2 post請求 form-data 格式的

這樣是post 表單傳參,這樣基本上也用來文件上傳
# 導(dǎo)入模塊
import requests
# 定義請求地址
url = 'https://dzw.news.qq.com/pet/send'
# 定義 fomedata 請求參數(shù)
m = MultipartEncoder(
fields={"uid":"4054942","gift":"4"}
)
# 定義自定義請求頭 并且制定類型
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36",
"Content-Type":m.content_type
}
# 使用 POST 請求參數(shù)發(fā)送請求
response = requests.post(url,headers=headers,data= m)
# 獲取響應(yīng)的 html 內(nèi)容
html = response.text
2.3 post 請求上傳文件和別的參數(shù)

這里是文件和dirCode 兩個參數(shù) 在post請求的時候 就要把他單獨來傳
# 導(dǎo)入模塊
import requests
# 定義請求地址
url = 'http://127.0.0.1:8183/oss/uploadFile'
headers = {
# 注意這里不能指定 Content-Type
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"
}
# 這里指定dirCode 別的參數(shù)
m = {
"dirCode": "1002"
}
# 這里指定文件
files = {'file': open('站點基礎(chǔ)數(shù)據(jù)錄入模板.xlsx', 'rb')}
# 使用 POST 請求參數(shù)發(fā)送請求
response = requests.post(url,headers = hearders, data= m,files = files)
# 獲取響應(yīng)的 html 內(nèi)容
html = response.text
2.4 post 請求 json 形式的(常用)

這個是最常用的json形式的傳參
# 導(dǎo)入模塊
import requests
# 定義請求地址
url = 'http://127.0.0.1:8183/notice/test'
headers = {
# 這里指定 Content-Type 是json 格式的
"Content-Type":"application/json",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"
}
# 指定參數(shù)
data = {
"id":"dzw",
"recipientPeopleList":["d6576263-4868-4420-a91b-17f3993582ff","e332a43a-9ab5-4827-a5f8-92acb2469bb9","d3cd8347-5a08-4c22-99ed-df153cbe6f41"]
}
# 發(fā)起請求 注意 data 是放在json 里面的
response = requests.post("http://127.0.0.1:8183/notice/test",headers = hearders, json = data)
# 打印參數(shù)
print(response.text)
三、總結(jié)
在使用這個庫進(jìn)行請求的時候,經(jīng)常會忘了之前的哪種請求怎么用的,然后踩了一些坑 正好這里寫下來記錄一下
到此這篇關(guān)于python3 requests 各種發(fā)送方式的文章就介紹到這了,更多相關(guān)python requests發(fā)送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python:解析requests返回的response(json格式)說明
- python requests response值判斷方式
- python中requests庫+xpath+lxml簡單使用
- Python爬蟲基礎(chǔ)之requestes模塊
- python爬蟲之利用Selenium+Requests爬取拉勾網(wǎng)
- Python requests timeout的設(shè)置
- 基于Python中request請求得到的response的屬性問題