您好,登錄后才能下訂單哦!
一、Requests模塊的作用。
Requests 的左右就是充當(dāng)http的客戶端,向服務(wù)端發(fā)送http請(qǐng)求,然后獲得對(duì)應(yīng)的響應(yīng)頭和響應(yīng)體。
二、包含的請(qǐng)求方式。
#請(qǐng)求方式:
#requests.post()
#requests.get()
#requests.delete()
#requests.head()
#requests.options()
三、基本用法。
response = requests.get("https://www.baidu.com") #向指定url發(fā)送get請(qǐng)求。
(response.text) #從服務(wù)端返回的response中獲取html文檔信息。 (response.status_code) #從服務(wù)端返回的response中獲取本次響應(yīng)的狀態(tài)碼。 (response.cookies) #從服務(wù)端獲得本次響應(yīng)的cookies。
(1)基本get請(qǐng)求。
#帶參數(shù)的get請(qǐng)求,有兩種傳遞參數(shù)的方式。 第一種方法: import requests response = requests.get(" #在本次的GET請(qǐng)求中一共傳了兩個(gè)參數(shù),分別是name = hamasaki age = 40 . print(response.text) 第二種方法: 另外一種傳參方式,就是通過(guò)生成dict,這種傳參的方式比較常用: import requests data = {"name":"hamasaki","age":40} response = requests.get("http://httpbin.org/get",params=data) print(response.text)
(2)通過(guò)Requests 獲取二進(jìn)制的數(shù)據(jù)。
import requests response = requests.get("https://githup.com/favicon.ico") ("favicon.ico","wb") as f: f.write(response.content)
(3)添加headers。
import requests
headers = { 'Content-Type':'application/json;charset=utf-8', ''Host':'www.baidu.com'} response = requests.get(url="www.baidu.com",headers=headers) print(response.text)
(4)基本post請(qǐng)求。
import requests
headers = {
'Content-Type': 'application/json;charset=utf-8',
'accept': '*/*',
'accept-encoding':'gzip, deflate, br',
'accept-language':'zh-CN,zh;q=0.9',
'access-control-request-headers':'content-type',
#'access-control-request-method:':'POST',
'origin': 'https://www.nike.com',
'referer':'https://www.nike.com/cn/zh_cn/e/nike-plus-membership',
'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36',
}
post_data = {
"client_id":"HlHa2Cje3ctlaOqnxvgZXNaAs7T9nAuH",
"grant_type":"password",
"password":"Suhaozhi123",
"username":"+861394227097",
"ux_id":"com.nike.commerce.nikedotcom.web"
}
response = requests.post(url="https://unite.nike.com/login)
print(response2.status_code)
(5)post請(qǐng)求上傳文件。
import requests
files = {'file':open('ayumi.jpg','rb')}
response = requests.post("http://httpbin.org/post",files=files)
(6)使用代理。
import requests
proxy_dict = { #普通http,https代理。
"http":"http://127.0.0.1:9743",
"https":"https://127.0.0.1:9743"
}
response = requests.get("https://www.baidu.com",proxies=proxy_dict)
print(response.status_code)
#入需輸入用戶名密碼的代理
import requests
proxy_dict = {
"http":"http://user:password@127.0.0.1:9743",
"https":"https://user:password@127.0.0.1:9743"
}
response = requests.get("https://www.baidu.com",proxies=proxy_dict)
print(response.status_code)
#socks代理
pip install 'requests[socks]'
import requests
proxy_dict = {
"http":"socks5://127.0.0.1:9743",
"https":"socks5://127.0.0.1:9743"
}
response = requests.get("https://www.baidu.com",proxies=proxy_dict)
print(response.status_code)
四、Respouse相關(guān)用法。
response.status_code #獲取狀態(tài)碼。
response.headers #獲取響應(yīng)頭。
response.cookies #獲取cookies對(duì)象。
response.url #獲取請(qǐng)求時(shí)的url。
response.history #獲取歷史記錄。
(1)獲取cookie
import requests
response = requests.get("https://www.baidu.com")
for k,v in response.cookies.items():
print(k+"="+v)
五、異常處理相關(guān):
import requests
from requests.exceptions import ReadTimeout,ConnectionError,RequestException
try:
response = requests.get("http://httpbin.org/get",timeour=0.5)
print(response.status_code)
except ReadTimeout:
print("Timeout")
except ConnectionError:
print("connection error")
except RequestException:
print("error")
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。