2013-05-03 56 views
1

我正在嘗試使用Python的BeautifulSoup庫進行一些簡單的網頁抓取,並且在嘗試解析大多數YouTube網頁時遇到UnicodeDecodeError。BeautifulSoup無法解析YouTube頁面

看來YouTube正在爲HTMl提供無效字符。當然,這是他們的一個問題,但我認爲BeautifulSoup的重點在於它可以處理不正確的頁面,並盡力猜測結果。如果它丟棄了無效字符,我會很高興。我遠離Unicode專家,我嘗試過的各種魔法咒語encodedecode對我沒有任何好處。

有沒有人對如何處理這個錯誤有任何建議。我不想讓我的代碼專用YouTube,因爲它需要處理大量用戶指定的網頁。

以下是一個演示問題非常簡單的代碼片段:

import urllib 
from bs4 import BeautifulSoup 
url='https://www.youtube.com/watch?v=W9MzrirPrCI' 
text = urllib.urlopen(url).read() 
soup = BeautifulSoup(text) 

最後一行導致以下錯誤:

UnicodeDecodeError      Traceback (most recent call last) 
/cygdrive/d/home/ll-virtualenv/lib/python2.7/site-packages/Django-1.5.1-py2.7.egg/django/core/management/commands/shell.pyc in <module>() 
----> 1 soup = BeautifulSoup(text) 

/cygdrive/d/home/ll-virtualenv/lib/python2.7/site-packages/bs4/__init__.pyc in __init__(self, markup, features, builder, parse_only, from_encoding, **kwargs) 
    170 
    171   try: 
--> 172    self._feed() 
    173   except StopParsing: 
    174    pass 

/cygdrive/d/home/ll-virtualenv/lib/python2.7/site-packages/bs4/__init__.pyc in _feed(self) 
    183   self.builder.reset() 
    184 
--> 185   self.builder.feed(self.markup) 
    186   # Close out any unfinished strings and close all the open tags. 
    187   self.endData() 

/cygdrive/d/home/ll-virtualenv/lib/python2.7/site-packages/bs4/builder/_lxml.pyc in feed(self, markup) 
    193  def feed(self, markup): 
    194   self.parser.feed(markup) 
--> 195   self.parser.close() 
    196 
    197  def test_fragment_to_document(self, fragment): 

/usr/lib/python2.7/site-packages/lxml-3.1.0-py2.7-cygwin-1.7.17-i686.egg/lxml/etree.dll in lxml.etree._FeedParser.close (src/lxml/lxml.etree.c:88786)() 

/usr/lib/python2.7/site-packages/lxml-3.1.0-py2.7-cygwin-1.7.17-i686.egg/lxml/etree.dll in lxml.etree._TargetParserContext._handleParseResult (src/lxml/lxml.etree.c:98085)() 

/usr/lib/python2.7/site-packages/lxml-3.1.0-py2.7-cygwin-1.7.17-i686.egg/lxml/etree.dll in lxml.etree._TargetParserContext._handleParseResult (src/lxml/lxml.etree.c:97909)() 

/usr/lib/python2.7/site-packages/lxml-3.1.0-py2.7-cygwin-1.7.17-i686.egg/lxml/etree.dll in lxml.etree._ExceptionContext._raise_if_stored (src/lxml/lxml.etree.c:9071)() 

/usr/lib/python2.7/site-packages/lxml-3.1.0-py2.7-cygwin-1.7.17-i686.egg/lxml/etree.dll in lxml.etree._handleSaxData (src/lxml/lxml.etree.c:94081)() 

UnicodeDecodeError: 'utf8' codec can't decode byte 0xd7 in position 22: invalid continuation byte 
+0

嘗試使用scrapy代替。 – 2013-05-03 16:46:05

+0

我使用的版本是4.1.3,它工作正常 – Moj 2013-05-03 17:05:29

+0

如果我回到BeautifulSoup的第3版,它的工作原理。 4.1.3仍然沒有給出上述錯誤。 Moj,你是否像我一樣使用相同的URL? – 2013-05-04 13:47:26

回答

0

進口beautifulsoup模塊之前嘗試sys.setdefaultencoding("utf-8")

+0

這並沒有幫助。 – 2013-05-04 13:45:55

+0

@ EddieSullivan,確保你的文件使用utf-8編碼。在模塊的開頭添加'#encoding:utf-8'。 ('#!/ usr/bin/python'後面) – richselian 2013-05-04 13:52:36

+0

問題不在於源代碼,而在於HTML頁面。 – 2013-05-06 18:24:41

1

BeautifulSoup 4到4.0.3版本都存在編碼問題,並且它已在最新版本中修復了一個錯誤。所以您應該將您的美麗湯升級到最新版本。

可以使用畫中畫升級美麗的湯最新版本(如果您正在使用PIP)

pip install beautifulsoup --upgrade 

在寫這個答案美麗的湯是4.3.2其中包含有關編碼沒有問題。

另外我試過你的代碼在最新的BS4上,並沒有這樣的問題。