最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(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)求的安裝與常見(jiàn)用法的相關(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等等,這里最常見(jiàn)的應(yīng)該就是get和post

二、requests安裝方式

$ pip install requests
$ easy_install requests

三、說(shuō)說(shuō)常見(jiàn)的兩種請(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 ,最常見(jiàn)的 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)求的安裝與常見(jiàn)用法的文章就介紹到這了,更多相關(guān)python中Requests請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python argparse模塊應(yīng)用實(shí)例解析

    Python argparse模塊應(yīng)用實(shí)例解析

    這篇文章主要介紹了Python argparse模塊應(yīng)用實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • python實(shí)現(xiàn)記事本功能

    python實(shí)現(xiàn)記事本功能

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)記事本功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • python3.4下django集成使用xadmin后臺(tái)的方法

    python3.4下django集成使用xadmin后臺(tái)的方法

    本篇文章主要介紹了python3.4下django集成使用xadmin后臺(tái)的方法,具有一定的參加價(jià)值,有興趣的可以了解一下
    2017-08-08
  • Python GUI編程 文本彈窗的實(shí)例

    Python GUI編程 文本彈窗的實(shí)例

    今天小編就為大家分享一篇Python GUI編程 文本彈窗的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • TensorFlow中如何確定張量的形狀實(shí)例

    TensorFlow中如何確定張量的形狀實(shí)例

    這篇文章主要介紹了TensorFlow中如何確定張量的形狀實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • 對(duì)python中arange()和linspace()的區(qū)別說(shuō)明

    對(duì)python中arange()和linspace()的區(qū)別說(shuō)明

    這篇文章主要介紹了對(duì)python中arange()和linspace()的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05
  • Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 錯(cuò)誤問(wèn)題解決方案

    Python 中 AttributeError: ‘NoneType‘ obje

    Python “AttributeError: ‘NoneType’ object has no attribute” 發(fā)生在我們嘗試訪問(wèn) None 值的屬性時(shí),例如 來(lái)自不返回任何內(nèi)容的函數(shù)的賦值, 要解決該錯(cuò)誤,請(qǐng)?jiān)谠L問(wèn)屬性之前更正分配,本文通過(guò)示例給大家說(shuō)明錯(cuò)誤是如何發(fā)生的,感興趣的朋友一起看看吧
    2023-08-08
  • nginx黑名單和django限速,最簡(jiǎn)單的防惡意請(qǐng)求方法分享

    nginx黑名單和django限速,最簡(jiǎn)單的防惡意請(qǐng)求方法分享

    今天小編就為大家分享一篇nginx黑名單和django限速,最簡(jiǎn)單的防惡意請(qǐng)求方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • Python如何刪除print()中的空格

    Python如何刪除print()中的空格

    這篇文章主要介紹了Python如何刪除print()中的空格問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python庫(kù)學(xué)習(xí)Tkinter制作GUI個(gè)性簽名設(shè)計(jì)軟件

    Python庫(kù)學(xué)習(xí)Tkinter制作GUI個(gè)性簽名設(shè)計(jì)軟件

    Tkinter 是 Python 中的標(biāo)準(zhǔn) GUI 庫(kù),使用 Tkinter 可以快速地創(chuàng)建 GUI 應(yīng)用程序。今天我們打算再用一個(gè)小案例,帶大家加深對(duì)Tkinter的理解
    2021-09-09

最新評(píng)論

墨竹工卡县| 炉霍县| 绍兴县| 绩溪县| 休宁县| 攀枝花市| 石棉县| 赣榆县| 疏附县| 大邑县| 温州市| 新竹市| 石首市| 台东市| 阿荣旗| 文成县| 都昌县| 竹山县| 美姑县| 沅江市| 忻城县| 潜江市| 清远市| 自治县| 慈溪市| 邢台市| 塘沽区| 富顺县| 沙坪坝区| 蒲江县| 茌平县| 罗平县| 滨海县| 久治县| 浦县| 麻城市| 林西县| 仁布县| 余姚市| 石嘴山市| 连平县|