使用lxml解析RSS时出现编码错误

时间:2011-04-27 23:44:12

标签: python rss lxml scraperwiki chardet

我想用lxml解析下载的RSS,但我不知道如何处理UnicodeDecodeError?

request = urllib2.Request('http://wiadomosci.onet.pl/kraj/rss.xml')
response = urllib2.urlopen(request)
response = response.read()
encd = chardet.detect(response)['encoding']
parser = etree.XMLParser(ns_clean=True,recover=True,encoding=encd)
tree = etree.parse(response, parser)

但是我收到了一个错误:

tree   = etree.parse(response, parser)
File "lxml.etree.pyx", line 2692, in lxml.etree.parse (src/lxml/lxml.etree.c:49594)
  File "parser.pxi", line 1500, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:71364)
  File "parser.pxi", line 1529, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:71647)
  File "parser.pxi", line 1429, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:70742)
  File "parser.pxi", line 975, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:67
740)
  File "parser.pxi", line 539, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etr
ee.c:63824)
  File "parser.pxi", line 625, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:64745)
  File "parser.pxi", line 559, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:64027)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 97: ordinal not in range(128)

3 个答案:

答案 0 :(得分:45)

我遇到了类似的问题,事实证明这与编码没有任何关系。发生了什么事--lxml给你一个完全不相关的错误。在这种情况下,错误是.parse函数需要文件名或URL,而不是具有内容本身的字符串。但是,当它尝试打印出错误时,它会对非ascii字符进行阻塞,并显示完全混淆错误消息。非常不幸,其他人在这里评论了这个问题:

https://mailman-mail5.webfaction.com/pipermail/lxml/2009-February/004393.html

幸运的是,你的解决方案非常简单。只需将.parse替换为.fromstring,你应该完全不错:

request = urllib2.Request('http://wiadomosci.onet.pl/kraj/rss.xml')
response = urllib2.urlopen(request)
response = response.read()
encd = chardet.detect(response)['encoding']
parser = etree.XMLParser(ns_clean=True,recover=True,encoding=encd)

## lxml Y U NO MAKE SENSE!!!
tree = etree.fromstring(response, parser)

刚刚在我的机器上测试了它,它工作正常。希望它有所帮助!

答案 1 :(得分:4)

首先更容易为lxml库加载和整理字符串,然后在其上调用fromstring,而不是依赖于lxml.etree.parse()函数及其难以管理的编码选项。

这个特定的rss文件以编码声明开头,所以一切都应该起作用:

<?xml version="1.0" encoding="utf-8"?>

以下代码显示了您可以应用于为不同编码进行etree解析的一些不同变体。您也可以请求它写出不同的编码,这些编码将显示在标题中。

import lxml.etree
import urllib2

request = urllib2.Request('http://wiadomosci.onet.pl/kraj/rss.xml')
response = urllib2.urlopen(request).read()
print [response]
        # ['<?xml version="1.0" encoding="utf-8"?>\n<feed xmlns=... <title>Wiadomo\xc5\x9bci...']

uresponse = response.decode("utf8")
print [uresponse]    
        # [u'<?xml version="1.0" encoding="utf-8"?>\n<feed xmlns=... <title>Wiadomo\u015bci...']

tree = lxml.etree.fromstring(response)
res = lxml.etree.tostring(tree)
print [res]
        # ['<feed xmlns="http://www.w3.org/2005/Atom">\n<title>Wiadomo&#347;ci...']

lres = lxml.etree.tostring(tree, encoding="latin1")
print [lres]
        # ["<?xml version='1.0' encoding='latin1'?>\n<feed xmlns=...<title>Wiadomo&#347;ci...']


# works because the 38 character encoding declaration is sliced off
print lxml.etree.fromstring(uresponse[38:])   

# throws ValueError(u'Unicode strings with encoding declaration are not supported.',)
print lxml.etree.fromstring(uresponse)

可以在这里尝试代码:     http://scraperwiki.com/scrapers/lxml_and_encoding_declarations/edit/#

答案 2 :(得分:0)

您应该只尝试将字符编码定义为最后的手段,因为很清楚编码是基于XML prolog的(如果不是HTTP标头)。无论如何,没有必要将编码传递给{ {1}}除非您想覆盖编码;所以摆脱etree.XMLParser参数,它应该工作。

编辑:好吧,问题实际上似乎与encoding有关。无论出于何种原因,以下工作:

lxml