python中Requests請(qǐng)求的安裝與常見用法
一、requests
request的說法網(wǎng)上有很多,簡(jiǎn)單來說就是就是python里的很強(qiáng)大的類庫(kù),可以幫助你發(fā)很多的網(wǎng)絡(luò)請(qǐng)求,比如get,post,put,delete等等,這里最常見的應(yīng)該就是get和post
二、requests安裝方式
$ pip install requests $ easy_install requests
三、說說常見的兩種請(qǐng)求,get和post
1、get請(qǐng)求
(1)參數(shù)直接跟在url后面,即url的“ ?”后面,以key=value&key=value的形式
(2)由于get的參數(shù)是暴露在外面的,所以一般不傳什么敏感信息,經(jīng)常用于查詢等操作
(3)由于參數(shù)是跟在url后面的,所以上傳的數(shù)據(jù)量不大
2、post請(qǐng)求
(1)參數(shù)可以寫在url后面,也可以寫在body里面
(2)用body上傳請(qǐng)求數(shù)據(jù),上傳的數(shù)據(jù)量比get大
(3)由于寫在body體里,相對(duì)安全
post正文格式
(1)form表單 html提交數(shù)據(jù)的默認(rèn)格式
Content-Type: application/x-www-form-urlencoded
例如: username=admin&password123
(2) multipart-form-data . 復(fù)合表單 可轉(zhuǎn)數(shù)據(jù)+文件
(3)純文本格式 raw ,最常見的 json . xml html js
Content-Type:application/json . text/xml . text/html
(4) binary . 二進(jìn)制格式:只能上傳一個(gè)文件
四、requests發(fā)送請(qǐng)求
1、requests發(fā)送get請(qǐng)求
url = "http://www.search:9001/search/"
param = {"key":"你好"}
res = requests.get(url=url, params=params)
2、request發(fā)送post請(qǐng)求 (body是json格式,如果還帶cookie)
headers = {'Content-Type': 'application/json'} #必須有
url = "http://www.search:9001/search/"
data= {"key":"你好"}
cookies = {"uid":"1"}
res = requests.post(url=url, headers=headers, data=data, cookies=cookies)
3、 request發(fā)送post請(qǐng)求 (body是urlencoded格式)
url = "http://www.search:9001/search/"
data= {"key":"你好"}
res = requests.post(url=url, headers=headers)4、 request上傳文件
def post_file_request(url, file_path):
if os.path.exists(file_path):
if url not in [None, ""]:
if url.startswith("http") or url.startswith("https"):
files = {'file': open(file_path, 'rb')}
res = requests.post(url, files=files, data=data)
return {"code": 0, "res": res}
else:
return {"code": 1, "res": "url格式不正確"}
else:
return {"code": 1, "res": "url不能為空"}
else:
return {"code": 1, "res": "文件路徑不存在"}
五、response
request發(fā)送請(qǐng)求后,會(huì)返回一個(gè)response,response里有好多信息,我進(jìn)行了一下封裝,基本如下
@staticmethod
def get_response_text(response):
if response not in [None, ""]:
if isinstance(response, requests.models.Response):
return {"code": 0, "res": response.text.encode('utf-8').decode('unicode_escape')} #這種方式可以將url編碼轉(zhuǎn)成中文,返回響應(yīng)文本
else:
return {"code": 1, "res": "response不合法"}
else:
return {"code": 1, "res": "response對(duì)像不能為空"}
@staticmethod
def get_response_status_code(response):
if response not in [None, ""]:
if isinstance(response, requests.models.Response):
return {"code": 0, "res": response.status_code} #返回響應(yīng)狀態(tài)嗎
else:
return {"code": 1, "res": "response不合法"}
else:
return {"code": 1, "res": "response對(duì)像不能為空"}
@staticmethod
def get_response_cookies(response):
if response not in [None, ""]:
if isinstance(response, requests.models.Response):
return {"code": 0, "res": response.cookies} #返回cookies
else:
return {"code": 1, "res": "response不合法"}
else:
return {"code": 1, "res": "response對(duì)像不能為空"}
@staticmethod
def get_response_headers(response):
if response not in [None, ""]:
if isinstance(response, requests.models.Response):
return {"code": 0, "res": response.headers} #返回headers
else:
return {"code": 1, "res": "response不合法"}
else:
return {"code": 1, "res": "response對(duì)像不能為空"}
@staticmethod
def get_response_encoding(response):
if response not in [None, ""]:
if isinstance(response, requests.models.Response):
return {"code": 0, "res": response.encoding} #返回編碼格式
else:
return {"code": 1, "res": "response不合法"}
else:
return {"code": 1, "res": "response對(duì)像不能為空"}補(bǔ)充:requests中遇到問題
獲取cookie
# -*- coding:utf-8 -*-
#獲取cookie
import requests
import json
url = "https://www.baidu.com/"
r = requests.get(url)
#將RequestsCookieJar轉(zhuǎn)換成字典
c = requests.utils.dict_from_cookiejar(r.cookies)
print(r.cookies)
print(c)
for a in r.cookies:
? ? print(a.name,a.value)
>> 控制臺(tái)輸出:
<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
{'BDORZ': '27315'}
BDORZ 27315發(fā)送Cookie
# -*- coding:utf-8 -*-
#發(fā)送cookie到服務(wù)器
import requests
import json
host = "*****"
endpoint = "cookies"
url = ''.join([host,endpoint])
#方法一:簡(jiǎn)單發(fā)送
# cookies = {"aaa":"bbb"}
# r = requests.get(url,cookies=cookies)
# print r.text
#方法二:復(fù)雜發(fā)送
s = requests.session()
c = requests.cookies.RequestsCookieJar()
c.set('c-name','c-value',path='/xxx/uuu',domain='.test.com')
s.cookies.update(c)?總結(jié)
到此這篇關(guān)于python中Requests請(qǐng)求的安裝與常見用法的文章就介紹到這了,更多相關(guān)python中Requests請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python使用requests發(fā)送POST請(qǐng)求實(shí)例代碼
- Python requests發(fā)送post請(qǐng)求的一些疑點(diǎn)
- python 使用 requests 模塊發(fā)送http請(qǐng)求 的方法
- python爬蟲使用requests發(fā)送post請(qǐng)求示例詳解
- python爬蟲 基于requests模塊的get請(qǐng)求實(shí)現(xiàn)詳解
- python爬蟲 基于requests模塊發(fā)起ajax的get請(qǐng)求實(shí)現(xiàn)解析
- python requests 庫(kù)請(qǐng)求帶有文件參數(shù)的接口實(shí)例
相關(guān)文章
Python實(shí)現(xiàn)的彩票機(jī)選器實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)彩票機(jī)選器的方法,可以模擬彩票號(hào)碼的隨機(jī)生成功能,需要的朋友可以參考下2015-06-06
matplotlib之Pyplot模塊繪制三維散點(diǎn)圖使用顏色表示數(shù)值大小
在撰寫論文時(shí)常常會(huì)用到matplotlib來繪制三維散點(diǎn)圖,下面這篇文章主要給大家介紹了關(guān)于matplotlib之Pyplot模塊繪制三維散點(diǎn)圖使用顏色表示數(shù)值大小的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
對(duì)Keras自帶Loss Function的深入研究
這篇文章主要介紹了對(duì)Keras自帶Loss Function的深入研究,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
Python如何使用cv2.canny進(jìn)行圖像邊緣檢測(cè)
這篇文章主要介紹了Python如何使用cv2.canny進(jìn)行圖像邊緣檢測(cè)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
淺談matplotlib.pyplot與axes的關(guān)系
這篇文章主要介紹了淺談matplotlib.pyplot與axes的關(guān)系,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

