Beautiful soup failing to parse this HTML

ぐ巨炮叔叔 提交于 2019-12-11 13:53:10

问题


We're using Beautiful Soup to parse many websites successfully, but a few are given us problems. An example is this page:

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

We're feeding the exact source to beautiful soup, but it returns a stunted HTML string, though no errors...

Code:

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

Result:

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

I'm trying to determine what's tripping it up, but nothing jumps out at me looking at the html source. Does anyone have some insight?


回答1:


Try different parsers, the page parses fine with the html5lib parser:

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

Not all parsers can treat broken HTML the same.



来源:https://stackoverflow.com/questions/15773977/beautiful-soup-failing-to-parse-this-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!