爬虫爬网站遇到编码器问题

学习爬虫遇到解析问题,爬取一个网站出现gbk问题,求指点。才学习爬虫不知道怎么处理这个问题。

代码:import requests

url = "https://www.xxxxx.html"

headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
}

resp = requests.post(url, headers=headers)

print(resp.text)

报错:Traceback (most recent call last):
File "D:\pythonProject\jjj.py", line 12, in
print(resp.text)
UnicodeEncodeError: 'gbk' codec can't encode character '\xa9' in position 26428: illegal multibyte sequence

成功了,加这一句

img

resp = requests.post(url, headers=headers)

print(resp.text)

这个就不能用text
print(resp.content.decode(‘’)) 换个别的试试
你爬的啥网址

要看页面源代码的这个charset=utf-8是什么,例如这个是utf-8那就resp.decode("utf-8")

别急着编码,先看看返回的数据有没有问题