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'))

你可能感兴趣的:(python爬虫头部怎么设置)