1、Requests 請求常用
- url:請求的 url 地址,介面文件標註的介面請求地址。
- params:請求資料中的連結,常見的一個 get 請求,請求引數都是在 url 地址中。
- data:請求資料,參數列單的資料格式。
- json:介面常見的資料請求格式。
- headers:請求頭資訊,http 請求中,比如說編碼方式等內容新增。
- cookie:儲存的使用者登入資訊,比如做一些充值功能,但是需要使用者已經登入,需要 cookie 資訊的請求資訊傳輸。
2、Requests 響應內容
- r.encoding:獲取當前編碼。
- r.encoding = 'utf-8':設定編碼
- r.text:以 encoding 解析返回內容。字串方式的響應體會自動根據響應頭部的字元編碼進行解碼。
- r.cookies:返回 cookie。
- r.headers:以字典物件儲存伺服器響應頭,但是這個字典鍵名不區分大小寫,若鍵不存在則返回 None。
- r.json():requests 中內建的 json 解碼器,以 json 形式返回。如果返回的內容不是 json 格式的,則解析時會出錯。
- r.content:以位元組形式返回。位元組形式的響應體,會自動解碼 gzip 和 deflate 壓縮。
3、平民寫法
import requests
url = 'https://www.baidu.com/s?ie=utf-8&mod=11&isbd=1&isid=ce9a8ff100aa1843&ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=hai&fenlei=256&oq=%25E5%25A4%25A9%25E6%25B6%25AF&rsv_pq=ce9a8ff100aa1843&rsv_t=66a6aZDMvWijVeIOfUCn4ILD2Dik38YoYEkf9D32fcnxNGd03Sm3S%2FMQ6g0&rqlang=cn&rsv_dl=tb&rsv_enter=0&rsv_btype=t&inputT=1805&rsv_sug3=11&rsv_sug1=6&rsv_sug7=100&bs=%E5%A4%A9%E6%B6%AF&rsv_sid=undefined&_ss=1&clist=&hsug=&csor=3&pstg=5&_cr1=32279'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36'
}
response = requests.get(url, headers=headers)
response.encoding = 'utf-8'
html = response.text
print(html)
4、官方寫法
import requests
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"^Cookie": "BIDUPSID=B20BBF8CADED8C221BE05F1B77C71B33; PSTM=1728719840; BD_UPN=12314753; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID=9422DA48AB773E237B906EF401BFC7FF:FG=1; H_PS_PSSID=60852_60619_60886_60875; BA_HECTOR=20a4202h2h8521212la5al2k852kc21jgmord1v; ZFY=y50eczm7S7EFalWbi2zKgGf8i8TJuBn:BzIMShfp6Wjs:C; BAIDUID_BFESS=9422DA48AB773E237B906EF401BFC7FF:FG=1; BD_CK_SAM=1; PSINO=3; delPer=0; baikeVisitId=7e84bf38-2e4e-4576-b20e-b917bb51e415; H_PS_645EC=7c8aGMvh6efFakEHuySOVhWopWm^%^2FKTAGqq2gfn6eGN^%^2FjV8dKUSTY2R34JNk; BDSVRTM=227; COOKIE_SESSION=16869_0_4_2_1_4_1_0_4_3_1_0_0_0_0_0_1728781267_0_1728800566^%^7C5^%^230_0_1728800566^%^7C1^",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
"^sec-ch-ua": "^\\^Google",
"sec-ch-ua-mobile": "?0",
"^sec-ch-ua-platform": "^\\^Windows^^^"
}
url = "https://www.baidu.com/"
params = {
"ie": "utf-8",
"f": "8",
"rsv_bp": "1",
"rsv_idx": "1",
"tn": "baidu",
"wd": "^%^E6^%^B5^%^B7^%^E8^%^B4^%^BC^%^E7^%^8E^%^8B",
"fenlei": "256",
"oq": "^%^25E6^%^25B5^%^25B7^%^25E8^%^25B4^%^25BC^%^25E7^%^258E^%^258B",
"rsv_pq": "fb5d927b002d11d5",
"rsv_t": "f64fMZ1xboMNI^%^2FvhKFlFZQPNDfM7tmq9MyPpsh3rSE8bsV2DByLxJc1^%^2BAHQ",
"rqlang": "cn",
"rsv_dl": "tb",
"rsv_enter": "0",
"rsv_btype": "t",
"rsv_sug": "1^"
}
response = requests.get(url, headers=headers, params=params)
print(response.text)
print(response)