最近在做一个爬虫,当爬取到网站后尝试对网站的编码进行设置,但却会出现报错
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