# -*- coding:utf-8 -*-
from urllib import request
req=request.urlopen("https://www.baidu.com/")
req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
resp=request.urlopen(req)
print(resp.read().decode('utf-8'))
报错
req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36")
AttributeError: 'HTTPResponse' object has no attribute 'add_header'
找到了
req=request.Request("https://www.baidu.com/")写错了。。。。