引入 requests 包
import requests
注:發(fā)送請(qǐng)求(包括請(qǐng)求行、方法類型、頭、體) & 常見(jiàn)的請(qǐng)求方式有g(shù)et、post、put、delete
一、發(fā)送get請(qǐng)求
? ? ? ? ? ?格式:requests.get() (內(nèi)容: url必填; params選填:url參數(shù)字典)
# ~ 無(wú)參數(shù)的get請(qǐng)求
res = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince')
print(res.text)
#打印響應(yīng)主體內(nèi)容,字符串格式
# ~ 有參數(shù)的get請(qǐng)求
res = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString?theRegionCode=3113')
print(res.text)
# ~ 使用params的get請(qǐng)求
res = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString',
params={"theRegionCode":3113})
print(res.text)
二、發(fā)送post請(qǐng)求
? ? ? ?知識(shí)擴(kuò)展# ?requests.post() post請(qǐng)求分為5種,常用的有三種,如下
? ? ? ①application/x-www-form-urlencod (form表單);
? ? ? ②raw (純文本格式):有5種格式,分別為json/xml/Html/Text/JavaScrip
? ? ? ③multipart/form-data (復(fù)合式表單)
# ~ 無(wú)參數(shù)的post請(qǐng)求 # ~ 有正文體的post請(qǐng)求
1. ? form表單(application/x-www-form-urlencod) ? ? ? ?
-----data ? 后跟字典
eg.1: 帶參數(shù)的查詢接口
res = requests.post(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString',
headers={"Content-Type": "application/x-www-form-urlencoded"},
data={"theRegionCode": 3113})
print(res.text)
??eg.2: 帶賬號(hào)名密碼的登錄接口
res = requests.post(url='http://123.56.99.53:9000/event/api/admin/',
headers={"Content-Type": "application/x-www-form-urlencoded"},
data={"username": "admin", "password": "MTIzYWRtaW4="})
print(res.text)
2.? ?純文本格式(raw)
?# ~ xml格式? ? ----data? ? 后跟字符串
res = requests.post(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx',
headers ={"Content-Type": "text/xml; charset=utf-8",
"SOAPAction": "http://WebXml.com.cn/getSupportCityString"},
data = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getSupportCityString xmlns="http://WebXml.com.cn/">
<theRegionCode>string</theRegionCode>
</getSupportCityString>
</soap:Body>
</soap:Envelope>''')
print(res.text)
??# ~json格式? ? ? ?-----?json? 后跟字典(常用), or ----data? 后跟json字符串
res = requests.post(url='http://123.56.99.53:5000/event/weather/getWeather/',
headers={"Content-Type": "application/json"},
json={"theCityCode": "1001"})
print(res.text)
注:# json后面跟的是字典即json={"theCityCode": "1001"}),可改成 data=‘{"theCityCode": "1001"})’;字典能自動(dòng)轉(zhuǎn)為json字符串,所以一般用字典表示;建議純json正文的接口使用第一種方式,除非是較長(zhǎng)的字符串 ? ??
3.? 復(fù)合式表單(multipart/form-data )??
? ------上傳二進(jìn)制文件? ? ----?key value (接口名稱:文件名)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-785058.html
res = requests.post(url='http://123.56.99.53:9001/api/uploadFile/',
headers={"Cookie": "uid=1;token=44c972f05d76fdd93c31f9c2b65bb098f308cdfc"
#"Content-Type": "multipart/form-data"},
#有的接口不需要寫正文體格式,老服務(wù)器一般需要寫
files={"myfile1": open('D:\全力以富\1.docx', 'rb')
#"myfile2: open(上傳多個(gè)文件)"})
print(res.text)
注:上傳接口 files={‘文件路徑’,‘rb'}? rb表示文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-785058.html
到了這里,關(guān)于(一)python發(fā)送HTTP 請(qǐng)求的兩種方式(get和post )的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!