python-3.x - Unicode编码错误: 'charmap' codec can't encode character '\u2010' : character maps to <undefined>

标签 python-3.x selenium encoding utf-8

当我尝试打印从使用 python 3.4 中的 selenium 请求的网站获得的“Á”时,我不断收到 UnicodeEncodeError 错误。

我已经在 .py 文件顶部定义了

# -*- coding: utf-8 -*-

def 是这样的:

from selenium import webdriver

b = webdriver.Firefox()
b.get('http://fisica.uniandes.edu.co/personal/profesores-de-planta')
dataProf = b.find_elements_by_css_selector('td[width="508"]')
for dato in dataProf:
        print(datos.text)

和异常(exception):

Traceback (most recent call last):
  File "C:/Users/Andres/Desktop/scrap/scrap.py", line 444, in <module>
    dar_p_fisica()
  File "C:/Users/Andres/Desktop/scrap/scrap.py", line 390, in dar_p_fisica
    print(datos.text) #.encode().decode('ascii', 'ignore')
  File "C:\Python34\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 '\u2010' in position 173: character maps to <undefined>

提前致谢

最佳答案

已经想通了。正如 this 中所述答案,编码错误不是来自python,而是来自控制台正在使用的编码。所以修复它的方法是运行命令(在Windows中):

chcp 65001

将编码设置为UTF-8,然后再次运行程序。或者,如果像我一样使用 pycharm,请转到“设置”>“编辑器”>“文件编码”并相应地设置 IDE 和项目编码。

关于python-3.x - Unicode编码错误: 'charmap' codec can't encode character '\u2010' : character maps to <undefined>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32382686/

相关文章:

java - 如何解决 "stale element reference: element is not attached to the page document when navigating to the previous page"

file - 具有此类数据的文件的格式或编码是什么?

python-3.x - 使用 PIL 保存图像时如何更改 dpi 或密度

python - 如何在python中的html电子邮件中添加python代码?

python - 如何将值和列名合并到新列(pandas)中?

c - 为什么多字节字符到 char32_t 的转换使用 UTF-8 作为多字节编码而不是特定于语言环境的编码?

php - mySQL 和 PHP 编码

python - 从类或实例设置属性时的查找过程是什么?

python - 使用 Python 中的 Selenium Webdriver 在后端运行 Firefox

css - 在测试用例上找不到 iframe