1. 程式人生 > >python爬蟲需要用到的偽裝頭部資訊

python爬蟲需要用到的偽裝頭部資訊

#模擬請求頭
headers = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "X-Requested-With": "XMLHttpRequest",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}
#使用者代理,使得伺服器能夠識別客戶使用的
作業系統
及版本 agentsList = [ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.3.4000 Chrome/30.0.1599.101 Safari/537.36", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0" ]
ag=random.choice(agentsList)
print(ag)
req=urllib.request.Request(path,headers=headers)
req.add_header('User-Agent',ag)
con=ssl._create_unverified_context() #程式忽略SSL證書驗證錯誤
data=urllib.request.urlopen(req,context=con)
print(data.read().decode('utf-8'))