本文介绍了UnicodeEncodeError: 'charmap' 编解码器无法编码字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试抓取网站,但出现错误.

我正在使用以下代码:

import urllib.request从 bs4 导入 BeautifulSoupget = urllib.request.urlopen("https://www.website.com/")html = get.read()汤 = BeautifulSoup(html)打印(汤)

我收到以下错误:

File "C:Python34libencodingscp1252.py", line 19, in encode返回 codecs.charmap_encode(input,self.errors,encoding_table)[0]UnicodeEncodeError: 'charmap' 编解码器无法对位置 70924-70950 中的字符进行编码:字符映射到 <undefined>

我该怎么做才能解决这个问题?

解决方案

我通过将 .encode("utf-8") 添加到 soup 来修复它.>

这意味着print(soup)变成了print(soup.encode("utf-8")).

I'm trying to scrape a website, but it gives me an error.

I'm using the following code:

import urllib.request
from bs4 import BeautifulSoup

get = urllib.request.urlopen("https://www.website.com/")
html = get.read()

soup = BeautifulSoup(html)

print(soup)

And I'm getting the following error:

File "C:Python34libencodingscp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 70924-70950: character maps to <undefined>

What can I do to fix this?

解决方案

I fixed it by adding .encode("utf-8") to soup.

That means that print(soup) becomes print(soup.encode("utf-8")).

这篇关于UnicodeEncodeError: 'charmap' 编解码器无法编码字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 16:12