问答详情
源自:3-1 urllib基本用法

req.add_header问题

# -*- 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'


提问者:Yuliya_Dai 2018-12-28 09:43

个回答

  • Yuliya_Dai
    2018-12-28 09:46:33

    找到了

    req=request.Request("https://www.baidu.com/")写错了。。。。