在我的代码中,我一直收到此错误...
UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 390: character maps to <undefined>
我试图除了UnicodeError和UnicodeEncodeError之外没有任何效果,问题是它是用户输入所以我无法控制它们放置的内容所以我需要所有编码错误来显示一个错误而不是崩溃的打印程序...
try:
argslistcheck = argslist[0]
if argslistcheck[0:7] != "http://":
argslist[0] = "http://" + argslist[0]
with urllib.request.urlopen(argslist[0]) as url:
source = url.read()
source = str(source, "utf8")
except urllib.error.URLError:
print("Couln't connect")
source = ""
except UnicodeEncodeError:
print("There was an error encrypting...")
source = ""
回溯:
Traceback (most recent call last):
..... things leading up to error
File "C:\path", line 99, in grab print(source)
File "C:\Python33\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 390: character maps to <undefined>
答案 0 :(得分:5)
您的打印失败。您的Windows控制台不支持打印UTF-8,您需要更改代码页:
chcp 65001
这是 Windows命令,而不是python命令。你可能也需要切换字体,Lucida Sans Console是一种可以处理更多字形的Unicode字体。
答案 1 :(得分:4)
答案 2 :(得分:3)
尝试这个替换str():
source = source.encode('UTF-8')
答案 3 :(得分:0)
start_url="https://www.indeed.co.in/jobs?q=teacher&l=India"
page_data=requests.get(start_url)
soup=BeautifulSoup(page_data.text,"lxml")
fname='1download'
with open(fname,'w')as f:
f.write(soup.prettify())
f.close()
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u20b9' in position
235677: character maps to <undefined>
通过将utf-8作为文件编码可以解决这两个错误。另外请注意,您必须使用open()方法打开文件
f = open()也会给您错误
这是正确的代码:
start_url="https://www.indeed.co.in/jobs?q=teacher&l=India"
page_data=requests.get(start_url)
soup=BeautifulSoup(page_data.text,"lxml")
fname='1download'
with open(fname,'w',encoding="utf-8")as f:
f.write(soup.prettify())
f.close()