python - BeautifulSoup 无法解析 YouTube 页面

标签 python beautifulsoup

我尝试使用 Python 的 BeautifulSoup 库进行一些简单的网页抓取,但在尝试解析大多数 YouTube 页面时遇到了 UnicodeDecodeError。

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

最佳答案

BeautifulSoup 4 到 4.0.3 版本都存在编码问题,该错误已在最新版本中修复。因此,您应该将 BeautifulSoup 升级到最新版本。

您可以使用 pip 将 beautiful soup 升级到最新版本(如果您正在使用 pip)

pip install beautifulsoup --upgrade

在写这个答案时,漂亮的汤是 4.3.2,其中不存在编码问题。

我还在最新的 bs4 上尝试了你的代码,没有这样的问题。

关于python - BeautifulSoup 无法解析 YouTube 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16363877/

相关文章:

python - 使用 Python 2.7 解析 HTML - HTMLParser、SGMLParser 或 Beautiful Soup?

python - 如何从 Python 中的字符串中删除\xa0?

python - BeautifulSoup 在每个开始和结束标签之前和之后添加额外的空白

python - 从 smg 文件 Beautiful Soup 和 Python 中提取正文标签

python - BeautifulSoup 和搜索结果

python - 使用 Python 从 aspx 页面下载 .xls 文件

python - 如何正确使用__setattr__,避免无限递归

python - 等效于 Python 中的 R 数组

python - 在列中创建相同的值,直到下一个日期

python - 用 BeautifulSoup 抓取不同的元素 : avoid duplicating in nested elements