資料內(nèi)容:
1.1 發(fā)送 HTTP 請(qǐng)求
? requests 模塊:最常用的 HTTP 請(qǐng)求庫(kù),支持 GET、POST、PUT、DELETE 等
方法。
import requests
# 發(fā)送 GET 請(qǐng)求
response = requests.get('https://example.com')
print(response.status_code) # 打印狀態(tài)碼
print(response.text) # 打印響應(yīng)內(nèi)容
1.2 設(shè)置請(qǐng)求頭
? headers:設(shè)置請(qǐng)求頭,偽裝瀏覽器,避免被網(wǎng)站識(shí)別為爬蟲。
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110
Safari/537.3'
}
response = requests.get('https://example.com',
headers=headers)
1.3 使用代理
? proxies:通過(guò)代理服務(wù)器發(fā)送請(qǐng)求,避免 IP 被封。
proxies = {
'http': 'http://10.10.1.10:3128',
'https': 'https://10.10.1.10:1080',
}
response = requests.get('https://example.com',
proxies=proxies)