Yar*_*rin 1 html python beautifulsoup html-parsing
我们使用Beautiful Soup成功解析了许多网站,但有一些网站出现了问题。一个例子是这个页面:
我们正在为美丽的汤提供确切的来源,但它返回一个矮小的 HTML 字符串,尽管没有错误......
代码:
soup = BeautifulSoup(site_html)
print str(soup.html)
Run Code Online (Sandbox Code Playgroud)
结果:
<html class="no-js" lang="en"> <!--<![endif]--> </html>
Run Code Online (Sandbox Code Playgroud)
我试图确定是什么让它绊倒了,但是看着 html 源代码,我没有任何反应。有没有人有一些见解?
尝试不同的解析器,页面使用html5lib解析器解析得很好:
>>> soup = BeautifulSoup(r.content, 'html5')
>>> len(soup.find_all('li'))
97
Run Code Online (Sandbox Code Playgroud)
并非所有解析器都可以相同地处理损坏的 HTML。