Python使用requests模塊發(fā)送http請求的方法介紹
1.引言
Python Requests是一個 HTTP 庫,它允許我們向 Web 服務器發(fā)送 HTTP 請求,并獲取響應結果。
requests: 讓 HTTP 服務人類 -- 來自requests文檔
它通過處理會話,cookie 的自動管理以及與 HTTP 連接的 Keep-Alive 能力來簡化了 HTTP 請求。 首先,我們需要安裝模塊。建議使用 pip,具體命令如下:pip install requests
。安裝完成后,我們就可以通過import requests
導入請求模塊并使用它了。
2.發(fā)送Get請求
使用 GET 方法獲取 Web 頁面
response = requests.get("https://www.poycode.cn/") print(response)
輸出結果:
<Response [200]>
看到上述結果,則表示我們請求成功了。如果你想查看更多關于response的信息,可以參考一下幾個方法
print(response.status_code) # 返回狀態(tài)碼 print(response.reason) # 正常返回OK,異常返回對應的Http響應狀態(tài)描述 print(response.headers) # 獲取響應頭 print(response.text) # 返回請求的內容 print(response.content) # 返回請求的內容 print(response.cookies) # cookies內容
關于 response.text
與 response.context
的區(qū)別:
方法 | 類型 | 解碼類型 | 變更編碼方式 |
---|---|---|---|
response.text | <class 'str'> | 根據(jù)HTTP 頭部對響應的編碼作出有根據(jù)的推測,推測的文本編碼 | response.encoding='gbk' |
response.content | <class 'bytes'> | 沒有指定 | response.content.deocde('utf-8') |
同時,我們還可以通過方法 requests.get(url, params, args)
攜帶參數(shù) 發(fā)送請求至對應地址:
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)
結果如下:
https://www.poycode.cn/?id=1024&name=poycode
寫個小示例,獲取二進制響應,抓取下poycode.cn站點上的圖片
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)
運行代碼后,即可在運行路徑下看到一張名為 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.代理服務器
requests模塊支持使用代理服務器發(fā)送HTTP請求??梢酝ㄟ^以下方式指定代理服務器:
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)信息,并且可以方便地獲取、設置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.總結
以上是 requests 模塊的一些基本操作示例。另外requests還能實現(xiàn)認證 requests.get(url, auth=('user','pwd'),此處不再演示。有需要或想要了解的可以通過查看 requests 的官方文檔,以便更好地了解它更高級的用法。
在本教程中,我們快速體驗并掌握 requests 模塊的基礎使用方法。這些足以保證我們應對日常的一些簡單開發(fā)工作,希望這篇教程能夠對你有所幫助!
到此這篇關于Python requests模塊發(fā)送http請求的方法的文章就介紹到這了,更多相關Python requests http請求內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python實現(xiàn)對excel中需要的數(shù)據(jù)的單元格填充顏色
這篇文章主要介紹了python實現(xiàn)對excel中需要的數(shù)據(jù)的單元格填充顏色,文章圍繞主題展開詳細單元格填充介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06Python實現(xiàn)統(tǒng)計文本文件字數(shù)的方法
這篇文章主要介紹了Python實現(xiàn)統(tǒng)計文本文件字數(shù)的方法,涉及Python針對文本文件讀取及字符串轉換、運算等相關操作技巧,需要的朋友可以參考下2017-05-05Python對Excel兩列數(shù)據(jù)進行運算的示例代碼
本文介紹了如何使用Python中的pandas庫對Excel表格中的兩列數(shù)據(jù)進行運算,并提供了詳細的代碼示例,感興趣的朋友跟隨小編一起看看吧2024-04-04