UnicodeDecodeError:'ascii'编解码器不能编码字符u'\ u2019'

时间:2014-01-31 19:37:31

标签: python python-2.7 unicode non-ascii-characters

我一直收到以下错误,似乎无法让.encode('ascii', errors='ignore')工作。

eqs = soup.find_all('div', {'style': 'margin:7px 5px 0px;vertical-align:top;text-align:center;display:inline-block;line-height:normal;width:120px;'})

for equipment in eqs:
    if '#b0c3d9' in str(equipment):
        f2.write(equipment.getText() + ', Common\n')
    if '#5e98d9' in str(equipment):
        f2.write(equipment.getText() + ', Uncommon\n')
    if '#4b69ff' in str(equipment):
        f2.write(equipment.getText() + ', Rare\n')
    if '#8847ff' in str(equipment):
        f2.write(equipment.getText() + ', Mythical\n')
    if '#b28a33' in str(equipment):
        f2.write(equipment.getText() + ', Immortal\n')
    if '#d32ce6' in str(equipment):
        f2.write(equipment.getText() + ', Legendary\n')
    if '#eb4b4b' in str(equipment):
        f2.write(equipment.getText() + ', Ancient\n')
    if '#ade55c' in str(equipment):
        f2.write(equipment.getText() + ', Arcana\n')

我试过了:

f2.write(equipment.getText().encode('ascii', errors='ignore'))

f2.write(equipment.encode('ascii', errors='ignore').getText())

除了其他一些我感到羞耻的事情。比如运行它通过BeautifulSoup稍后会读取的文件,但这只会引发不同的错误。再次感谢您的帮助。

完全追溯:

Traceback (most recent call last):
 File "<pyshell#285>", line 1, in <module>
  import D2soup1
 File "D2soup1.py", line 86, in <module>
  test()
 File "D2soup1.py", line 30, in test
  f2.write(equipment.getText() + ', Immortal\n')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 5:     ordinal not in range(128)

我正在使用字符串来解析下面的html中的box-shadow。我知道这可能不是最好的做法,但这是我能想到的唯一方法。还是BeautifulSoup的新手。

<div style="margin:7px 5px 0px;vertical-align:top;text-align:center;display:inline-block;line-height:normal;width:120px;"><div style="margin-bottom: 5px;box-shadow:0px 0px 2px 4px #5e98d9;"><a href="/Pirate_Slayer%27s_Tricorn" title="Pirate Slayer's Tricorn"><img alt="Pirate Slayer's Tricorn" src="http://hydra-media.cursecdn.com/dota2.gamepedia.com/thumb/7/79/Pirate_Slayer%27s_Tricorn.png/120px-Pirate_Slayer%27s_Tricorn.png" width="120" height="80" srcset="http://hydra-media.cursecdn.com/dota2.gamepedia.com/thumb/7/79/Pirate_Slayer%27s_Tricorn.png/180px-Pirate_Slayer%27s_Tricorn.png 1.5x, http://hydra-media.cursecdn.com/dota2.gamepedia.com/thumb/7/79/Pirate_Slayer%27s_Tricorn.png/240px-Pirate_Slayer%27s_Tricorn.png 2x"></a></div>

1 个答案:

答案 0 :(得分:3)

您在没有编解码器的情况下使用str(equipment);您正在将Tag对象编码为ASCII。

不要使用str;获取文本一次作为unicode值。并使用映射和循环而不是那么多if语句。

在这种情况下,您需要测试style属性:

types = {
    '#b0c3d9': 'Common',
    '#5e98d9': 'Uncommon',
    '#4b69ff':'Rare',
    '#8847ff': 'Mythical',
    '#b28a33': 'Immortal',
    '#d32ce6': 'Legendary',
    '#eb4b4b': 'Ancient',
    '#ade55c': 'Arcana'
}

for equipment in eqs:
    style = equipment.div.attrs.get('style', '')
    textcontent = equipment.getText().encode('utf8')
    for key in types:
        if key in style:
            f2.write('{}, {}'.format(textcontent, types[key])

然而,最有可能的是,这些颜色代码位于equipment标记的属性中;只查看代码值,或使用.find()调用缩小搜索范围。