基本的反爬措施
# 添加 header 信息, 这是最基本的反爬措施
url = "https://www.dianping.com/"
header = {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
}
req = request.Request(url,headers=header)
res = request.urlopen(url) #获取响应
print(res.info()) # 响应头
print(res.getcode()) # 状态码
print(res.geturl()) #返回响应地址