python?包?requests?實(shí)現(xiàn)請(qǐng)求操作
一、安裝
pip install requests
二、請(qǐng)求類型
import requests
requests.get('https://www.baidu.com')
requests.post('https://www.baidu.com')
requests.put('https://www.baidu.com')
requests.delete('https://www.baidu.com')
requests.head('https://www.baidu.com')
requests.options('https://www.baidu.com')三、帶參數(shù)請(qǐng)求
import requests
data = {
'name': 'autofelix',
'age': 25
}
response = requests.get('https://www.baidu.com', params=data)
print(response.url)
print(response.text)四、自定義headers
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'
}
response =requests.get('https://www.baidu.com', headers=headers)
print(response.text)五、請(qǐng)求屬性
import requests
response = requests.get('https://www.baidu.com')
# 響應(yīng)狀態(tài)碼
response.status_code
# 響應(yīng)頭
response.headers
# 響應(yīng)cookie
response.cookies
# 請(qǐng)求url
response.url
# 歷史記錄
response.history六、文件上傳
import requests
files= {"files":open("git.jpeg","rb")}
response = requests.post('https://www.baidu.com/upload', files=files)七、會(huì)話維持
import requests
s = requests.Session()
s.get('https://www.baidu.com/login')
response = s.get('https://www.baidu.com')八、證書驗(yàn)證
verify設(shè)置False關(guān)閉證書驗(yàn)證- urllib3可以解決
InsecureRequestWarning提示
import requests
from requests.packages import urllib3
urllib3.disable_warnings()
response = requests.get('https://www.12306.cn', verify=False)九、代理設(shè)置
import requests
proxies= {
'http': 'http://127.0.0.1:9999',
'https': 'http://127.0.0.1:8888'
}
response = requests.get('https://www.baidu.com', proxies=proxies)十、超時(shí)設(shè)置
import requests
requests.get('https://www.baidu.com', timeout=3)十一、認(rèn)證設(shè)置
import requests
response = requests.get('https://www.baidu.com', auth=('user', '123'))十二、異常處理
ReadTimeout是超時(shí)錯(cuò)誤ConnectionError是網(wǎng)絡(luò)錯(cuò)誤
import requests
from requests.exceptions import ReadTimeout,ConnectionError,RequestException
try:
response = requests.get('https://www.baidu.com', timout=0.1)
print(response.status_code)
except ReadTimeout:
print('timeout')
except ConnectionError:
print('connection Error')
except RequestException:
print('error')到此這篇關(guān)于python 包 requests 實(shí)現(xiàn)請(qǐng)求操作的文章就介紹到這了,更多相關(guān)python equests 請(qǐng)求操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python接口自動(dòng)化淺析requests請(qǐng)求封裝原理
- Python接口自動(dòng)化之淺析requests模塊get請(qǐng)求
- 詳解python requests中的post請(qǐng)求的參數(shù)問題
- python 實(shí)現(xiàn)Requests發(fā)送帶cookies的請(qǐng)求
- python中requests模擬登錄的三種方式(攜帶cookie/session進(jìn)行請(qǐng)求網(wǎng)站)
- python爬蟲使用requests發(fā)送post請(qǐng)求示例詳解
- Python接口自動(dòng)化之request請(qǐng)求封裝源碼分析
相關(guān)文章
Django中日期處理注意事項(xiàng)與自定義時(shí)間格式轉(zhuǎn)換詳解
這篇文章主要給大家介紹了關(guān)于Django中日期處理注意事項(xiàng)與自定義時(shí)間格式轉(zhuǎn)換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
python定時(shí)任務(wù) sched模塊用法實(shí)例
這篇文章主要介紹了python定時(shí)任務(wù) sched模塊用法實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
淺談sklearn中predict與predict_proba區(qū)別
這篇文章主要介紹了淺談sklearn中predict與predict_proba區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
python中出現(xiàn)invalid?syntax報(bào)錯(cuò)的幾種原因分析
這篇文章主要介紹了python中出現(xiàn)invalid?syntax報(bào)錯(cuò)的幾種原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Flask應(yīng)用部署與多端口管理實(shí)踐全指南
在開發(fā)和部署Web應(yīng)用時(shí),開發(fā)者常常需要處理多端口服務(wù),防火墻配置以及生產(chǎn)環(huán)境優(yōu)化等問題,下面小編就來和大家簡(jiǎn)單講講Flask應(yīng)用部署與多端口管理實(shí)踐的相關(guān)知識(shí)吧2025-04-04

