在window下学习编写python脚本时,遇到了UnicodeEncodeError: ‘gbk’ codec can’t encode character的问题。
with open('duanzi.txt','w') as f:
for cont in content:
f.write(cont)
出现如下错误:
Traceback (most recent call last):
File "duanzi.py", line 16, in
f.write(cont)
UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 8: illegal multibyte sequence
经分析,这个问题的原因是open(‘duanzi.txt’,‘w’)由于window下在创建的duanzi.txt的编码方式为gbk,而我们爬取的内容编码格式是utf-8。
问题解决方法:
在创建这个文件时,设置其编码方式为utf-8,即可通过。
with open('duanzi.txt','w',encoding='utf-8') as f:
for cont in content:
f.write(cont)