gpt4 book ai didi

python - 在Python中抓取错误: 'charmap' codec can't encode character/can't concat str to bytes

转载 作者:行者123 更新时间:2023-12-01 01:46:53 24 4
gpt4 key购买 nike

当我尝试从“url”中抓取一些带有 Finish-Names 的文本时,出现上述错误。我尝试过的解决方案和相应的错误,在代码中注释如下。我既不知道如何解决这些问题,也不知道确切的问题是什么。我是 Python 初学者。任何帮助表示赞赏。

我的代码:

from lxml import html
import requests

page = requests.get('url')

site = page.text # ERROR -> 'charmap' codec can't encode character '\x84' in
# position {x}: character maps to <undefined>
# site = site.encode('utf-8', errors='replace') # ERROR -> can't concat str to bytes
# site = site.encode('ascii', errors='replace') # ERROR -> can't concat str to bytes

with open('url.txt', 'a') as file:
try:
file.write(site + '\n')
except Exception as err:
file.write('an ERROR occured: ' + str(err) + '\n')

和原始异常:

Traceback (most recent call last):
File "...\parse.py", line 12, in <module>
file.write(site + '\n') File
"...\python36\lib\encodings\cp1252.py", line 19, in encode return
codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\x84' in position
12591: character maps to <undefined>

问候,多米尼克

最佳答案

试试这个

with open('url.txt', 'a',encoding='utf-8') as file:

关于python - 在Python中抓取错误: 'charmap' codec can't encode character/can't concat str to bytes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51230307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com