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

Python使用requests模塊發(fā)送http請求的方法介紹

 更新時(shí)間:2023年06月26日 10:11:54   作者:陸理手記  
Python?Requests是一個(gè)?HTTP?庫,它允許我們向?Web?服務(wù)器發(fā)送??HTTP?請求,并獲取響應(yīng)結(jié)果,本文將會(huì)詳細(xì)介紹Python?requests模塊如何發(fā)送http請求,文中有相關(guān)的代碼示例,需要的朋友可以參考下

1.引言

Python Requests是一個(gè) HTTP 庫,它允許我們向 Web 服務(wù)器發(fā)送  HTTP 請求,并獲取響應(yīng)結(jié)果。

requests: 讓 HTTP 服務(wù)人類 -- 來自requests文檔

它通過處理會(huì)話,cookie 的自動(dòng)管理以及與 HTTP 連接的 Keep-Alive 能力來簡化了 HTTP 請求。 首先,我們需要安裝模塊。建議使用 pip,具體命令如下:pip install requests。安裝完成后,我們就可以通過import requests 導(dǎo)入請求模塊并使用它了。

2.發(fā)送Get請求

使用 GET 方法獲取 Web 頁面

response = requests.get("https://www.poycode.cn/")
print(response)

輸出結(jié)果:

<Response [200]>

看到上述結(jié)果,則表示我們請求成功了。如果你想查看更多關(guān)于response的信息,可以參考一下幾個(gè)方法

print(response.status_code) # 返回狀態(tài)碼
print(response.reason) # 正常返回OK,異常返回對應(yīng)的Http響應(yīng)狀態(tài)描述
print(response.headers)  # 獲取響應(yīng)頭
print(response.text) # 返回請求的內(nèi)容
print(response.content) # 返回請求的內(nèi)容
print(response.cookies) # cookies內(nèi)容

關(guān)于 response.textresponse.context 的區(qū)別:

方法類型解碼類型變更編碼方式
response.text<class 'str'>根據(jù)HTTP 頭部對響應(yīng)的編碼作出有根據(jù)的推測,推測的文本編碼response.encoding='gbk'
response.content<class 'bytes'>沒有指定response.content.deocde('utf-8')

同時(shí),我們還可以通過方法 requests.get(url, params, args) 攜帶參數(shù) 發(fā)送請求至對應(yīng)地址:

url = 'https://www.poycode.cn/'
params = {'id': 1024, 'name':'poycode'}
response = requests.get("https://www.poycode.cn/", params) # args為其他參數(shù),如header、cookies之類,此處不演示
#打印完整請求地址
print(response.url)

結(jié)果如下:

https://www.poycode.cn/?id=1024&name=poycode

寫個(gè)小示例,獲取二進(jìn)制響應(yīng),抓取下poycode.cn站點(diǎn)上的圖片

response = requests.get("https://static.poycode.cn/wp-content/uploads/2023/06/20230602225540172.jpg")
print(response.content)
with open ('poycode.png', 'wb') as f:
    f.write(response.content)

運(yùn)行代碼后,即可在運(yùn)行路徑下看到一張名為 poycode.png 的圖片。

3. 使用 POST 方法提交表單數(shù)據(jù)

payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('https://httpbin.org/post', data=payload)
print(response.content)

還可以使用 JSON 數(shù)據(jù)提交 POST 請求

import json
payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('https://httpbin.org/post', json=payload)
print(response.content)

4.代理服務(wù)器

requests模塊支持使用代理服務(wù)器發(fā)送HTTP請求。可以通過以下方式指定代理服務(wù)器:

import requests
url = 'http://www.poycode.cn'
proxies = {
    'http': 'http://proxy.poycode.cn:8080',
    'https': 'http://proxy.poycode.cn:8080',
}
response = requests.get(url, proxies=proxies)

5.使用cookies

requests模塊支持使用cookies來保存登錄信息等用戶狀態(tài)信息,并且可以方便地獲取、設(shè)置cookies。

import requests
url = 'https://www.poycode.cn'
response = requests.get(url)
print(response.cookies) # 獲取cookies
#直接使用cookies
cookies = repsonse.cookies
resp = requests.post("https://www.poycode.cn", cookies=cookies)
#或者將cookies放入headers中
headers = {"Set-Cookie": cookies}
resp = requests.post("https://www.poycode.cn", headers=headers)

6.其他協(xié)議

除了HTTP協(xié)議,requests模塊還支持使用FTP協(xié)議、HTTPS協(xié)議、SMTP協(xié)議等??梢酝ㄟ^以下方式來使用其他協(xié)議:

import requests
url = 'ftp://poycode.cn/file'
response = requests.get(url)
url = 'https://poycode.cn'
response = requests.get(url)
url = 'smtp://poycode.cn'
response = requests.post(url)

7.總結(jié)

以上是 requests 模塊的一些基本操作示例。另外requests還能實(shí)現(xiàn)認(rèn)證 requests.get(url, auth=('user','pwd'),此處不再演示。有需要或想要了解的可以通過查看 requests 的官方文檔,以便更好地了解它更高級的用法。

在本教程中,我們快速體驗(yàn)并掌握 requests 模塊的基礎(chǔ)使用方法。這些足以保證我們應(yīng)對日常的一些簡單開發(fā)工作,希望這篇教程能夠?qū)δ阌兴鶐椭?/p>

到此這篇關(guān)于Python requests模塊發(fā)送http請求的方法的文章就介紹到這了,更多相關(guān)Python requests http請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論