UnicodeEncodeError: 'charmap' codec can't encode character 'u2010': character maps to <undefined> [英] UnicodeEncodeError: 'charmap' codec can't encode character 'u2010': character maps to <undefined>

查看:49
本文介绍了UnicodeEncodeError: 'charmap' codec can't encode character 'u2010': character maps to <undefined>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试打印从在 python 3.4 中使用 selenium 请求的网站获取的Á"时,我不断收到 UnicodeEncodeError.

I keep getting UnicodeEncodeError when trying to print a 'Á' that I get from a website requested using selenium in python 3.4.

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

I already defined at the top of my .py file

# -*- 编码:utf-8 -*-

定义是这样的:

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)

和例外:

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:Python34libencodingscp1252.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 中):

Already figured it out. As it is noted in this answer, the encoding error doesnt come from python, but from the encoding that the console is using. So the way to fix it is to run the command (in windows):

chcp 65001

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

that sets the encoding to UTF-8 and then run the program again. Or if working on pycharm as I was, go to Settings>Editor>File Encodings and set the IDE and Project encodings accondingly.

这篇关于UnicodeEncodeError: 'charmap' codec can't encode character 'u2010': character maps to &lt;undefined&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆