2011-09-27 40 views
0

我有一個RSS源shich在GB2312如何分析與GB2312編碼RSS在Python

編碼當我嘗試使用下面的代碼來分析它:

for item in XML.ElementFromURL(feed).xpath('//item'): 
    title = item.find('title').text 

這是不能夠解析飼料。

任何想法如何解析GB2312編碼的RSS提要

從Plex的媒體服務器錯誤日誌下面是使用編碼如下

for item in XML.ElementFromURL(feed, encoding='gb2312').xpath('//item'): 
     title = item.find('title').text 

後:

***Error Log:*** 
> File "C:\Documents and Settings\subhendu.swain\Local Settings\Application Data\Plex Media Server\Plug-ins\Zaobao.bundle\Contents\Code\__init__.py", line 24, in GetDetails 
    for item in XML.ElementFromURL(feed, encoding='gb2312').xpath('//item'): 
    File "C:\Documents and Settings\subhendu.swain\Local Settings\Application Data\Plex Media Server\Plug-ins\Framework.bundle\Contents\Resources\Versions\2\Python\Framework\api\parsekit.py", line 81, in ElementFromURL 
    return self.ElementFromString(self._core.networking.http_request(url, values, headers, cacheTime, autoUpdate, encoding, errors, immediate=True, sleep=sleep, opener=self._opener, txn_id=self._txn_id).content, isHTML=isHTML) 
    File "C:\Documents and Settings\subhendu.swain\Local Settings\Application Data\Plex Media Server\Plug-ins\Framework.bundle\Contents\Resources\Versions\2\Python\Framework\api\parsekit.py", line 76, in ElementFromString 
    return self._core.data.xml.from_string(string, isHTML) 
    File "C:\Documents and Settings\subhendu.swain\Local Settings\Application Data\Plex Media Server\Plug-ins\Framework.bundle\Contents\Resources\Versions\2\Python\Framework\components\data.py", line 134, in from_string 
    return etree.fromstring(markup) 
    File "lxml.etree.pyx", line 2532, in lxml.etree.fromstring (src/lxml/lxml.etree.c:48270) 
    File "parser.pxi", line 1545, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:71812) 
    File "parser.pxi", line 1424, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:70673) 
    File "parser.pxi", line 938, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:67442) 
    File "parser.pxi", line 539, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:63824) 
    File "parser.pxi", line 625, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:64745) 
    File "parser.pxi", line 565, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:64088) 
XMLSyntaxError: switching encoding: encoder error, line 1, column 36 

2011-09-28 09:34:33,453 (9d0) : DEBUG (core) - Response: 404 
+0

我無法想象爲什麼你刪除了回溯 - 這是至關重要的信息。我已經回滾你的編輯。請不要再刪除它。 –

回答

1

我假設你是使用Plex XML API。該文檔指出,如果您知道這確實是正在使用的編碼,則可以致電XML.ElementFromURL(feed, encoding='gb2312')

如果XML確實是用GB2312編碼的,那麼聲明必須是<?xml version="1.0" encoding="gb2312"?>(或者以UTF-16的字節順序標記開頭),否則爲the XML is invalid。如果在XML聲明中沒有encoding,並且沒有字節順序標記,則默認情況下,解析器必須採用UTF-8編碼,因此在聲明中使用任何其他字符編碼而不使用encoding是無效的。由於未指定編碼會給您帶來錯誤,所以我認爲RSS提要可能不是有效的XML。

+0

是的飼料包含編碼信息在開始: <?xml version =「1.0」encoding =「GB2312」?>,謝謝,我正在嘗試你的解決方案 – Simsons

+0

如果明確指定編碼修復它,我會懷疑Plex中的一個錯誤。如果這樣做,請考慮參與他們在這個問題上的支持論壇。 – wberry

+0

仍然無法使用XML.ElementFromURL解析(feed,encoding ='gb2312') – Simsons

2

您的錯誤消息是XMLSyntaxError: switching encoding: encoder error, line 1, column 36。你問了一些想法。這裏有一個新穎的想法:告訴我們「第一行」的前50個字節是什麼。然後有人可能會想出一種補救辦法。

更新:編碼聲明不正確。數據不在gb2312中編碼。它至少是GBK又名cp936。 GB2312-80(即1980年的80)是一個有限的字符集。不使用UTF-8的中文網站將至少使用超級GBK(已使用超過10年)並轉移到超級超級GB103030(本身是UTF)。見下:

[Python 2.7.1] 
>>> import urllib 
>>> url = "http://www.zaobao.com/sp/sp.xml" 
>>> data = urllib.urlopen(url).read() 
>>> len(data) 
10071 
>>> data[:100] 
'<?xml version="1.0" encoding="GB2312"?>\n\n<rss version="2.0"\n>\n\n<channel>\n<title>\xc1\xaa\xba\xcf\xd4\xe7\xb1\xa8\xcd\xf8 zaobao.co' 
>>> x = data.decode('gb2312') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
UnicodeDecodeError: 'gb2312' codec can't decode bytes in position 1771-1772: illegal multibyte sequence 
>>> data[1771:1773] 
'\x95N' 
>>> x = data.decode('utf8') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\python27\lib\encodings\utf_8.py", line 16, in decode 
    return codecs.utf_8_decode(input, errors, True) 
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc1 in position 80: invalid start byte 
>>> x = data.decode('gbk') 
>>> y = data.decode('cp936') 
>>> x == y 
True 

我建議你試試XML.ElementFromURL(feed, encoding='gbk')

如果有效,您可能希望通過使用urllib讀取數據,檢查gb2312並且如果發現它,請使用gb18030來代替防止這種非常見問題的代碼。

更新2:如果有人提到chardet:由於GBK使用GB2312中的許多未使用的插槽,並且chardet沒有在實際使用的插槽上工作,並且未嘗試通過嘗試解碼來驗證其答案,因此charget會推測GB2312 。

+0

您的意思是什麼是XML的第1行?如果是這樣,那麼到RSS的鏈接是http://www.zaobao.com/sp/sp.xml – Simsons