请问出现这样的编码问题应该怎么解决呢
import requests# ------------------爬取带参数的get请求-------------------爬取新浪新闻,指定的内容
# 1.寻找基础url
base_url = 'https://search.sina.com.cn/?'
# 2.设置headers字典和params字典,再发请求
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
}
key = '孙悟空'# 搜索内容
params = {
'q': key,
'c': 'news',
'from': 'channel',
'ie': 'utf-8',
}
response = requests.get(base_url, headers=headers, params=params)
with open('sina_news.html', 'w', encoding='gbk') as fp:
fp.write(response.content.decode('gbk'))
这是网上教学的源代码
Traceback (most recent call last):
File "D:\学习\python\爬虫实战\新浪新闻.py", line 14, in <module>
fp.write(response.content.decode('gbk'))
UnicodeDecodeError: 'gbk' codec can't decode byte 0xba in position 956: illegal multibyte sequence
这是报错的原因,请问这应该是如何解决呢
改成 utf-8
页:
[1]