美丽的汤无法解析此HTML

时间:2013-04-02 20:43:43

标签: python html-parsing beautifulsoup

我们正在使用Beautiful Soup成功解析许多网站,但有一些网站给我们带来了问题。这个页面就是一个例子:

http://www.designsponge.com/2013/04/biz-ladies-how-to-use-networking-to-improve-your-search-engine-rankings.html

我们正在为精美的汤提供精确的来源,但它会返回一个发育不良的HTML字符串,但没有错误......

代码:

soup = BeautifulSoup(site_html)
print str(soup.html)

结果:

<html class="no-js" lang="en"> <!--&lt;![endif]--> </html>

我正在试图确定是什么绊倒它,但是没有什么能在我看到html源代码时跳出来。有没有人有一些见解?

1 个答案:

答案 0 :(得分:4)

尝试使用不同的解析器,页面可以使用html5lib解析器解析:

>>> soup = BeautifulSoup(r.content, 'html5')
>>> len(soup.find_all('li'))
97

并非所有解析器都可以对待损坏的HTML。