BeautifulSoup解析器
在我们使用BeautifulSoup的时候,选择怎样的解析器是至关重要的。使用不同的解析器有可能会出现不同的结果!
今天遇到一个坑,在解析某html的时候。使用html.parser解析器自己将table标签截断了(当然这与html本身有直接关系)
原html如下
<table cellpadding="0" cellspacing="0" style="WIDTH: 426.85pt; BORDER-COLLAPSE: collapse; MARGIN-LEFT: 0pt">
<tbody>
<tr>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 24.55pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: middle; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt; FONT-WEIGHT: bold">排序</span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 93.75pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: middle; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt; FONT-WEIGHT: bold">中标候选人名称</span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 80.55pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: middle; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt; FONT-WEIGHT: bold">投标报价</span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 87.45pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: middle; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt; FONT-WEIGHT: bold">质量</span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 85.8pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: middle; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt; FONT-WEIGHT: bold">工期/交货期</span>
</p>
</td>
</tr>
<tr style="HEIGHT: 23.35pt">
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 24.55pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt">1</span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 93.75pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
<p style="TEXT-JUSTIFY: inter-ideograph; TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体; FONT-SIZE: 12pt">西安大唐电信有限公司</span>
</p>
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"></p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 80.55pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
<p style="TEXT-JUSTIFY: inter-ideograph; TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体; FONT-SIZE: 12pt">RMB8,029,283.00</span><span style="FONT-FAMILY: 宋体; FONT-SIZE: 12pt">元</span>
</p>
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"></p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 87.45pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
<p style="TEXT-JUSTIFY: inter-ideograph; TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体; FONT-SIZE: 12pt">合格</span>
</p>
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"></p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 85.8pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
<p style="TEXT-JUSTIFY: inter-ideograph; TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体; FONT-SIZE: 12pt">满足招标文件要求</span>
</p>
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"></p>
</td>
</tr>
<tr>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 24.55pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt">2</span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 93.75pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 80.55pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 87.45pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 85.8pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
</td>
</tr>
<tr>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 24.55pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt">3</span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 93.75pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 80.55pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 87.45pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 85.8pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
</td>
</tr>
</tbody>
</table>
渲染效果为
排序 |
中标候选人名称 |
投标报价 |
质量 |
工期/交货期 |
1 |
西安大唐电信有限公司 |
RMB8,029,283.00元 |
合格 |
满足招标文件要求 |
2 |
||||
3 |
但是使用html.parser解释器的话html修改为了
<table cellpadding="0" cellspacing="0" style="WIDTH: 426.85pt; BORDER-COLLAPSE: collapse; MARGIN-LEFT: 0pt">
<tbody>
<tr>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 24.55pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: middle; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt; FONT-WEIGHT: bold">排序</span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 93.75pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: middle; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt; FONT-WEIGHT: bold">中标候选人名称</span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 80.55pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: middle; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt; FONT-WEIGHT: bold">投标报价</span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 87.45pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: middle; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt; FONT-WEIGHT: bold">质量</span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 85.8pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: middle; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt; FONT-WEIGHT: bold">工期/交货期</span>
</p>
</td>
</tr>
<tr style="HEIGHT: 23.35pt">
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 24.55pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="TEXT-ALIGN: center; LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt">1</span>
</p>
</td>
<td style="BORDER-BOTTOM: #000000 0.75pt solid; BORDER-LEFT: #000000 0.75pt solid; PADDING-LEFT: 5.03pt; WIDTH: 93.75pt; PADDING-RIGHT: 5.03pt; VERTICAL-ALIGN: top; BORDER-TOP: #000000 0.75pt solid; BORDER-RIGHT: #000000 0.75pt solid">
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"><span style="FONT-SIZE: 10.5pt"> </span>
</p>
<p style="TEXT-JUSTIFY: inter-ideograph; TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: 'Calibri','sans-serif'; FONT-SIZE: 10.5pt"><span style="FONT-FAMILY: 宋体; FONT-SIZE: 12pt">西安大唐电信有限公司</span>
</p>
<p style="LINE-HEIGHT: 150%; WIDOWS: 0; MARGIN: 0pt; ORPHANS: 0; FONT-SIZE: 10.5pt"></p>
</td>
</tr>
</tbody>
</table>
渲染效果为
排序 |
中标候选人名称 |
投标报价 |
质量 |
工期/交货期 |
1 |
西安大唐电信有限公司 |
BeautifulSoup官方推荐使用lxml
推荐使用lxml作为解析器,因为效率更高. 在Python2.7.3之前的版本和Python3中3.2.2之前的版本,必须安装lxml或html5lib, 因为那些Python版本的标准库中内置的HTML解析方法不够稳定.
提示: 如果一段HTML或XML文档格式不正确的话,那么在不同的解析器中返回的结果可能是不一样的,查看 解析器之间的区别 了解更多细节