python中Requests請(qǐng)求的安裝與常見用法
一、requests
request的說法網(wǎng)上有很多,簡單來說就是就是python里的很強(qiáng)大的類庫,可以幫助你發(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])
#方法一:簡單發(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 庫請(qǐng)求帶有文件參數(shù)的接口實(shí)例
相關(guān)文章
Python argparse模塊應(yīng)用實(shí)例解析
這篇文章主要介紹了Python argparse模塊應(yīng)用實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
python3.4下django集成使用xadmin后臺(tái)的方法
本篇文章主要介紹了python3.4下django集成使用xadmin后臺(tái)的方法,具有一定的參加價(jià)值,有興趣的可以了解一下2017-08-08
對(duì)python中arange()和linspace()的區(qū)別說明
這篇文章主要介紹了對(duì)python中arange()和linspace()的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python 中 AttributeError: ‘NoneType‘ obje
Python “AttributeError: ‘NoneType’ object has no attribute” 發(fā)生在我們嘗試訪問 None 值的屬性時(shí),例如 來自不返回任何內(nèi)容的函數(shù)的賦值, 要解決該錯(cuò)誤,請(qǐng)?jiān)谠L問屬性之前更正分配,本文通過示例給大家說明錯(cuò)誤是如何發(fā)生的,感興趣的朋友一起看看吧2023-08-08
nginx黑名單和django限速,最簡單的防惡意請(qǐng)求方法分享
今天小編就為大家分享一篇nginx黑名單和django限速,最簡單的防惡意請(qǐng)求方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python庫學(xué)習(xí)Tkinter制作GUI個(gè)性簽名設(shè)計(jì)軟件
Tkinter 是 Python 中的標(biāo)準(zhǔn) GUI 庫,使用 Tkinter 可以快速地創(chuàng)建 GUI 應(yīng)用程序。今天我們打算再用一個(gè)小案例,帶大家加深對(duì)Tkinter的理解2021-09-09

