from bs4 import BeautifulSoup
import urllib.request
link = ('https://mywebsite.org')
req = urllib.request.Request(link, headers={'User-Agent': 'Mozilla/5.0'})
url = urllib.request.urlopen(req).read()
soup = BeautifulSoup(url, "html.parser")
body = soup.find_all('div', {"class":"wrapper"})
print(body)嗨,伙计们,我对这段代码有问题。如果我运行它,就会出现错误。
UnicodeEncodeError:“charmap”编解码器无法对138号位置中的字符\u2022进行编码:字符映射到
我试着去寻找,我发现我不得不加上
.encode("utf-8")
但是如果我加上它,就会出现错误
AttributeError:'ResultSet‘对象没有属性'encode’
我怎么能解决这个问题?
我为我的英语感到抱歉,但我是意大利人
发布于 2016-03-18 14:12:16
您正在Windows上尝试打印到控制台。print()正在抛出异常。
Windows本机只支持8位代码页,因此您区域以外的任何内容都会中断(不管人们怎么说chcp 65001)。
您需要安装和使用https://github.com/Drekin/win-unicode-console。这个模块在底层与控制台API对话,支持多字节字符.
或者,不要打印到控制台并将输出写到一个文件中,该文件是用编码打开的。例如:
with open("myoutput.log", "w", encoding="utf-8") as my_log:
my_log.write(body)https://stackoverflow.com/questions/36086399
复制相似问题