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)文章
用python簡單實(shí)現(xiàn)mysql數(shù)據(jù)同步到ElasticSearch的教程
今天小編就為大家分享一篇用python簡單實(shí)現(xiàn)mysql數(shù)據(jù)同步到ElasticSearch的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python實(shí)現(xiàn)GIF動(dòng)圖以及視頻卡通化詳解
本文主要介紹了如何使用Python中的animegan2-pytorch實(shí)現(xiàn)動(dòng)圖以及視頻的卡通化效果,文中的代碼具有一定的學(xué)習(xí)價(jià)值,需要的朋友可以參考一下2021-12-12
python如何實(shí)現(xiàn)內(nèi)容寫在圖片上
這篇文章主要為大家詳細(xì)介紹了python如何實(shí)現(xiàn)內(nèi)容寫在圖片上,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
超詳細(xì),教你用python語言實(shí)現(xiàn)QQ機(jī)器人制作教程
這篇文章主要介紹了如何python語言實(shí)現(xiàn)QQ機(jī)器人,用圖文詳細(xì)的描述了其中的操作步驟,非常的簡單易上手,有需要的朋友可以參考下2021-08-08

