python爬虫在爬取页面时,修改页面编码出现问题AttributeError: 'NoneType' object has no attribute 'apparent_encoding'

最近在做一个爬虫,当爬取到网站后尝试对网站的编码进行设置,但却会出现报错
    f = gethtml(url)
    f.encoding = f.apparent_encoding

在运行过程中,偶然的出现错误 :
AttributeError: 'NoneType' object has no attribute 'apparent_encoding'
目前没有发现错误出现的原因,请问这是什么问题
附上gethtml函数的代码
def gethtml( url):
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'
    }
    try:
        html = requests.get(url,headers = headers,timeout = 2)
        return html
    except requests.exceptions.RequestException:
        print('请求超时')
        return

那是因为gethtml函数中请求超时或其他原因,抛出异常,返回了None 值 。在调用时出现None类型无apparent_encoding属性。你可以将timeout设置大些。

如解答对你有帮助,请点击下采纳按钮~

f.encoding = f.apparent_encoding这个赋值就是错误的
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632