BeautifulSoup HTMLParseError.这有什么问题?

use*_*496 4 python beautifulsoup

这是我的代码:

from bs4 import BeautifulSoup as BS
import urllib2
url = "http://services.runescape.com/m=news/recruit-a-friend-for-free-membership-and-xp"
res = urllib2.urlopen(url)
soup = BS(res.read())
other_content = soup.find_all('div',{'class':'Content'})[0]
print other_content
Run Code Online (Sandbox Code Playgroud)

但是出现了一个错误:

/Library/Python/2.7/site-packages/bs4/builder/_htmlparser.py:149: RuntimeWarning: Python's built-in HTMLParser cannot parse the given document. This is not a bug in Beautiful Soup. The best solution is to install an external parser (lxml or html5lib), and use Beautiful Soup with that parser. See http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser for help.
  "Python's built-in HTMLParser cannot parse the given document. This is not a bug in Beautiful Soup. The best solution is to install an external parser (lxml or html5lib), and use Beautiful Soup with that parser. See http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser for help."))
Traceback (most recent call last):
  File "web.py", line 5, in <module>
    soup = BS(res.read())
  File "/Library/Python/2.7/site-packages/bs4/__init__.py", line 172, in __init__
    self._feed()
  File "/Library/Python/2.7/site-packages/bs4/__init__.py", line 185, in _feed
    self.builder.feed(self.markup)
  File "/Library/Python/2.7/site-packages/bs4/builder/_htmlparser.py", line 150, in feed
    raise e
Run Code Online (Sandbox Code Playgroud)

我让其他两个人使用这个代码,它对他们非常好.为什么它不适合我?我安装了bs4 ......

Roc*_*key 6

根据错误消息,您可能需要做的一件事就是安装lxml,这将为BeautifulSoup提供更强大的解析引擎.有关更好的概述,请参阅文档中的部分,但它可能适用于其他两个人的原因是他们已安装lxml(或另一个正确处理HTML的解析器),这意味着BeautifulSoup使用它而不是标准的内置(旁注:您的示例对我来说也适用于lxml已安装的系统,但如果没有安装,则会失败).

另外,请参阅文档中的此注释:

如果您使用的是早于2.7.3的Python 2版本,或者早于3.2.2的Python 3版本,则必须安装lxml或html5lib-Python的内置HTML解析器在旧版本中不是很好版本.

我建议运行sudo apt-get install python-lxml并查看问题是否仍然存在.