爬取网页文件报错:UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xe7’ in position 295: illegal multibyte sequence
就是编码问题 文件打开以utf-8编码打开
import requests
r = requests.get("http://www.baidu.com", timeout = 3)
r.encoding = r.apparent_encoding
if r.status_code == 200:
f = open("baiduHTML.txt", "w" ,encoding='utf-8')
str = r.text
f.write(str)
else:
print("404 NOT FOUND")