Python 打印到命令Shell不起作用时重音和umlaut的处理

Python 打印到命令Shell不起作用时重音和umlaut的处理,python,scrapy,Python,Scrapy,我正在尝试使用scrapy将一些源代码中的页面标题打印到命令提示符/行中。如果任何字符是非标准字符(即带有重音、元音等),则会导致错误。我已尝试使用以下方法: myheader = titles.extract()[0] myheader = str(myheader) print '********** Page Title:', myheader.decode('utf-8'), '**********' 而且 myheader = titles.extract()[0]

我正在尝试使用scrapy将一些源代码中的页面标题打印到命令提示符/行中。如果任何字符是非标准字符(即带有重音、元音等),则会导致错误。我已尝试使用以下方法:

myheader = titles.extract()[0]
myheader = str(myheader)
print '********** Page Title:', myheader.decode('utf-8'), '**********'
而且

myheader = titles.extract()[0]
        myheader = str(myheader)
        print '********** Page Title:', myheader.decode(), '**********'
myheader = titles.extract()[0]
        myheader = str(myheader)
        print '********** Page Title:', myheader.encode('utf-8'), '**********'
而且

myheader = titles.extract()[0]
        myheader = str(myheader)
        print '********** Page Title:', myheader.decode(), '**********'
myheader = titles.extract()[0]
        myheader = str(myheader)
        print '********** Page Title:', myheader.encode('utf-8'), '**********'
我试图打印的导致此错误的示例如下:

<meta name="title" content="Mustang Cup Liga Postobón Clausura tables, rankings & standings | WhoScored.com">
exceptions.UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 23: ordinal not in range(128)
有人能告诉我为什么我的上述方法都不起作用吗

谢谢

  • Selector.extract()
    始终返回unicode的列表
  • 小心使用
    str
    它将使用默认编码(ascii)进行编码
  • 因此,正确的做法是:

    myheader=titles.extract()[0]
    打印u'*******页面标题:{}*********'.格式(myheader).encode('utf-8')
    
    您遇到了什么错误?@PadraicCunningham hi再次修改了问题以显示错误。谢谢你在哪里运行代码?@PadraicCunningham在命令shell中