您好,登錄后才能下訂單哦!
一、Requests模塊的作用。
Requests 的左右就是充當http的客戶端,向服務端發送http請求,然后獲得對應的響應頭和響應體。
二、包含的請求方式。
#請求方式:
#requests.post()
#requests.get()
#requests.delete()
#requests.head()
#requests.options()
三、基本用法。
response = requests.get("https://www.baidu.com") #向指定url發送get請求。
(response.text) #從服務端返回的response中獲取html文檔信息。 (response.status_code) #從服務端返回的response中獲取本次響應的狀態碼。 (response.cookies) #從服務端獲得本次響應的cookies。
(1)基本get請求。
#帶參數的get請求,有兩種傳遞參數的方式。 第一種方法: import requests response = requests.get(" #在本次的GET請求中一共傳了兩個參數,分別是name = hamasaki age = 40 . print(response.text) 第二種方法: 另外一種傳參方式,就是通過生成dict,這種傳參的方式比較常用: import requests data = {"name":"hamasaki","age":40} response = requests.get("http://httpbin.org/get",params=data) print(response.text)
(2)通過Requests 獲取二進制的數據。
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請求。
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請求上傳文件。
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相關用法。
response.status_code #獲取狀態碼。
response.headers #獲取響應頭。
response.cookies #獲取cookies對象。
response.url #獲取請求時的url。
response.history #獲取歷史記錄。
(1)獲取cookie
import requests
response = requests.get("https://www.baidu.com")
for k,v in response.cookies.items():
print(k+"="+v)
五、異常處理相關:
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")
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。