LaG*_*lle 6 python lxml beautifulsoup html-parsing web-scraping
我正在尝试使用BeautifulSoup从网页中提取表格的HTML代码.
<table class="facts_label" id="facts_table">...</table>
Run Code Online (Sandbox Code Playgroud)
我想知道为什么代码波纹管与作品"html.parser"背面,并打印none如果我改变"html.parser"了"lxml".
#! /usr/bin/python
from bs4 import BeautifulSoup
from urllib import urlopen
webpage = urlopen('http://www.thewebpage.com')
soup=BeautifulSoup(webpage, "html.parser")
table = soup.find('table', {'class' : 'facts_label'})
print table
Run Code Online (Sandbox Code Playgroud)
小智 16
简短的回答。
如果您已经安装lxml,只需使用它。
html.parser -BeautifulSoup(markup, "html.parser")
优点:包括电池,速度不错,宽松(从 Python 2.7.3 和 3.2 开始。)
缺点:不是很宽容(在 Python 2.7.3 或 3.2.2 之前)
lxml -BeautifulSoup(markup, "lxml")
优点:非常快,宽大
缺点:外部 C 依赖
html5lib -BeautifulSoup(markup, "html5lib")
优点:极其宽松,以与 Web 浏览器相同的方式解析页面,创建有效的 HTML5
缺点:非常慢,外部 Python 依赖