Python 美化组HtmlPasserError。什么';这有什么不对?
这是我的代码:Python 美化组HtmlPasserError。什么';这有什么不对?,python,beautifulsoup,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
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
然而,出现了一个错误:
/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
/Library/Python/2.7/site packages/bs4/builder/_htmlparser.py:149:RuntimeWarning:Python的内置htmlparser无法解析给定文档。这不是美汤里的虫子。最好的解决方案是安装一个外部解析器(lxml或html5lib),并与该解析器一起使用BeautifulSoup。看见http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser寻求帮助。
“Python的内置HTMLParser无法解析给定文档。这不是Beauty Soup中的错误。最好的解决方案是安装外部解析器(lxml或html5lib),并将Beauty Soup与该解析器一起使用。请参阅http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser以获取帮助。“))
回溯(最近一次呼叫最后一次):
文件“web.py”,第5行,在
soup=BS(res.read())
文件“/Library/Python/2.7/site-packages/bs4/_-init__.py”,第172行,在_-init中__
self._feed()
文件“/Library/Python/2.7/site-packages/bs4/_-init___uuuu.py”,第185行,在feed中
self.builder.feed(self.markup)
文件“/Library/Python/2.7/site packages/bs4/builder/_htmlparser.py”,第150行,在提要中
提高e
我已经让另外两个人使用了这个代码,它对他们非常有效。为什么它对我不起作用?我已经安装了bs4…根据错误消息,您可能需要做的一件事是安装
lxml
,这将为BeautifulSoup提供更强大的解析引擎。有关更好的概述,请参见文档中的一节,但它适用于另外两个人的可能原因是他们安装了lxml
(或另一个正确处理HTML的解析器),这意味着BeautifulSoup使用它而不是标准内置的(旁注:您的示例在安装了lxml
的系统上也适用,但在没有安装的系统上失败)
此外,请参见文档中的此注释:
如果您使用的是早于2.7.3的Python 2版本,或者
对于早于3.2.2的Python3,安装lxml是至关重要的
或者html5lib–Python内置的HTML解析器在应用程序中不是很好
旧版本
我建议运行
sudo-apt-get-install-python-lxml
,看看问题是否仍然存在。@chm它在我的系统上是正确的,没有任何更改,但是我已经安装了lxml
——当我在没有它的系统上尝试它时,我会得到相同的错误。原因是有些HTML对于内置PAR来说太糟糕了要处理的er(事物lxml
可以更好地理解)-如果出现这种情况,并且没有更好的选项可用,那么它将失败,您将看到错误。我在我的bs4包中找到bs4/builder/_lxml.py,但是如果我在代码中使用import lxml
,我将得到ImportError。您好,感谢提示。我尝试键入sudo apt get install python lxml,但它显示“apt get”找不到命令。不管怎样,我正在使用其他方法与您的链接provided@user1915496对不起,应该先问一下你是否在Linux/etc上。如果这些方法不能解决问题,请告诉我,我们可以尝试另一种方法。祝你一切顺利!