he3080611091 发表于 2023-3-16 20:57:10

求助UnicodeEncodeError

import requests

res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')

print(res.text)
print(res.status_code)
res.encoding = 'uniqude'

file = open('book.html', 'a+')

file.write(res.text)

file.close()

出现
Traceback (most recent call last):
File "F:/python_s/demo2_1.py", line 14, in <module>
    file.write(res.text)
UnicodeEncodeError: 'gbk' codec can't encode character '\xa9' in position 3738: illegal multibyte sequence错误,这个如何解决呢

liuhongrun2022 发表于 2023-3-16 21:00:10

import requests

res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')

print(res.text)
print(res.status_code)
res.encoding = 'utf-8'

file = open('book.html', 'a+', encoding='utf-8')

file.write(res.text)

file.close()

sfqxx 发表于 2023-3-16 21:20:14

liuhongrun2022 发表于 2023-3-16 21:00


厉害

ba21 发表于 2023-3-16 21:25:38


import requests
import cchardet

res = requests.get('https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html')

# 判断编码
content = res.content
enc = cchardet.detect(content)
enc = enc['encoding']
print(enc)

# 以指定编码写入
file = open('book.html', 'a+', encoding=enc)

file.write(res.text) # res.text

file.close()

歌者文明清理员 发表于 2023-3-16 22:02:30

这是编码问题。open默认以gbk编码,而网页一般是utf-8
在open函数里加上encoding='utf-8'就可以了

哦对了你的代码因为没来得及关文件
所以如果还报错请重启电脑,把原来的文件删掉
页: [1]
查看完整版本: 求助UnicodeEncodeError