欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中Requests請(qǐng)求的安裝與常見用法

 更新時(shí)間:2022年07月07日 09:58:49   作者:你是猴子請(qǐng)來(lái)的救兵嗎!!  
Requests是一常用的http請(qǐng)求庫(kù),它使用python語(yǔ)言編寫,可以方便地發(fā)送http請(qǐng)求,以及方便地處理響應(yīng)結(jié)果,下面這篇文章主要給大家介紹了關(guān)于python中Requests請(qǐng)求的安裝與常見用法的相關(guān)資料,需要的朋友可以參考下

一、requests

request的說(shuō)法網(wǎng)上有很多,簡(jiǎn)單來(lái)說(shuō)就是就是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

三、說(shuō)說(shuō)常見的兩種請(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中遇到問(wèn)題

獲取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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 利用python3如何給數(shù)據(jù)添加高斯噪聲

    利用python3如何給數(shù)據(jù)添加高斯噪聲

    高斯噪聲既是符合高斯正態(tài)分布的誤差,一些情況下我們需要向標(biāo)準(zhǔn)數(shù)據(jù)中加入合適的高斯噪聲會(huì)讓數(shù)據(jù)變得有一定誤差而具有實(shí)驗(yàn)價(jià)值,下面這篇文章主要給大家介紹了關(guān)于利用python3如何給數(shù)據(jù)添加高斯噪聲的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • Python實(shí)現(xiàn)的彩票機(jī)選器實(shí)例

    Python實(shí)現(xiàn)的彩票機(jī)選器實(shí)例

    這篇文章主要介紹了Python實(shí)現(xiàn)彩票機(jī)選器的方法,可以模擬彩票號(hào)碼的隨機(jī)生成功能,需要的朋友可以參考下
    2015-06-06
  • python的dataframe和matrix的互換方法

    python的dataframe和matrix的互換方法

    下面小編就為大家分享一篇python的dataframe和matrix的互換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Python處理PDF及生成多層PDF實(shí)例代碼

    Python處理PDF及生成多層PDF實(shí)例代碼

    Python提供了眾多的PDF支持庫(kù),本篇文章主要介紹了Python處理PDF及生成多層PDF實(shí)例代碼,這樣就能夠?qū)崿F(xiàn)圖片掃描上來(lái)的內(nèi)容也可以進(jìn)行內(nèi)容搜索的目標(biāo)
    2017-04-04
  • matplotlib之Pyplot模塊繪制三維散點(diǎn)圖使用顏色表示數(shù)值大小

    matplotlib之Pyplot模塊繪制三維散點(diǎn)圖使用顏色表示數(shù)值大小

    在撰寫論文時(shí)常常會(huì)用到matplotlib來(lái)繪制三維散點(diǎn)圖,下面這篇文章主要給大家介紹了關(guān)于matplotlib之Pyplot模塊繪制三維散點(diǎn)圖使用顏色表示數(shù)值大小的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • 對(duì)Keras自帶Loss Function的深入研究

    對(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è)

    這篇文章主要介紹了Python如何使用cv2.canny進(jìn)行圖像邊緣檢測(cè)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 淺析Python中線程以及線程阻塞

    淺析Python中線程以及線程阻塞

    這篇文章主要為大家簡(jiǎn)單介紹一下Python中線程以及線程阻塞的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下
    2023-04-04
  • 淺談matplotlib.pyplot與axes的關(guān)系

    淺談matplotlib.pyplot與axes的關(guān)系

    這篇文章主要介紹了淺談matplotlib.pyplot與axes的關(guān)系,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Python 復(fù)平面繪圖實(shí)例

    Python 復(fù)平面繪圖實(shí)例

    今天小編就為大家分享一篇Python 復(fù)平面繪圖實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11

最新評(píng)論