使用Python和Beautiful Soup从HTML表中删除`<br/>`和``

时间:2018-07-05 09:57:18

标签: python web-scraping html-table beautifulsoup

我想使用Python和bs4从抓取的HTML表中删除<img data-interchange="[images/church-small.jpg, small], [images/church-medium.jpg, medium], [images/church-large.jpg, large]" class="home-banner__image" > <br>

HTML表格:

&nbsp;

Python代码:

    <tr>
    <td style="width: 15; BORDER-BOTTOM: 1px solid">col1</td>
    <td colspan="2" style="width: 120; BORDER-BOTTOM: 1px solid">&nbsp;col2</td>
    <td style="width: 50; BORDER-BOTTOM: 1px solid">col3</td>
    <td style="width: 50; BORDER-BOTTOM: 1px solid">col5</td>
    <td style="width: 50; BORDER-BOTTOM: 1px solid">col6</td>
    <td style="width: 90; BORDER-BOTTOM: 1px solid" align="center">col7</td>
    <td style="width: 90; BORDER-BOTTOM: 1px solid" align="center">col8</td>
    <td style="width: 10; BORDER-BOTTOM: 1px solid">col9</td>
    <td style="width: 10; BORDER-BOTTOM: 1px solid">col
        <br>&nbsp;1
        <br>0</td>
    <td style="width: 10; BORDER-BOTTOM: 1px solid">col11</td>
    <td style="width: 10; BORDER-BOTTOM: 1px solid" >col12</td>
    <td style="width: 10; BORDER-BOTTOM: 1px solid">col13</td>
    <td style="width: 10; BORDER-BOTTOM: 1px solid">col14</td>
    <td style="width:10;BORDER-BOTTOM: 1px solid;" >col15</td>
</tr>
<tr bordercolor="#000000" class="rows1">
    <td align="left">&nbsp;1</td>
    <td colspan="2" style="BORDER-LEFT: 1px solid" align="left">&nbsp;123456789</td>
    <td style="BORDER-LEFT: 1px solid" align="left">&nbsp;John </td>
    <td style="BORDER-LEFT: 1px solid" align="left">&nbsp;Doe </td>
    <td style="BORDER-LEFT: 1px solid" align="left">&nbsp; </td>
    <td style="BORDER-LEFT: 1px solid" align="right">&nbsp;3.000</td>
    <td style="BORDER-LEFT: 1px solid" align="right">&nbsp;0,00</td>
    <td style="BORDER-LEFT: 1px solid" align="right">&nbsp;30</td>
    <td style="BORDER-LEFT: 1px solid" align="right">&nbsp;0</td>
    <td style="BORDER-LEFT: 1px solid" align="right">&nbsp;</td>
    <td style="BORDER-LEFT: 1px solid" align="right">&nbsp;</td>
    <td style="BORDER-LEFT: 1px solid; BORDER-RIGHT: 1px solid" align="right">&nbsp;</td>
    <td style="BORDER-LEFT: 1px solid; BORDER-RIGHT: 1px solid" align="right">&nbsp;</td>
    <td style="BORDER-LEFT: 1px solid;BORDER-RIGHT: 1px solid;" align="right">&nbsp;5000</td>
</tr>
<tr bordercolor="#000000" class="rows0">
    <td align="left">&nbsp;2</td>
    <td colspan="2" style="BORDER-LEFT: 1px solid" align="left">&nbsp;123456789</td>
    <td style="BORDER-LEFT: 1px solid" align="left">&nbsp;Jane </td>
    <td style="BORDER-LEFT: 1px solid" align="left">&nbsp;Doe </td>
    <td style="BORDER-LEFT: 1px solid" align="left">&nbsp; </td>
    <td style="BORDER-LEFT: 1px solid" align="right">&nbsp;3.000</td>
    <td style="BORDER-LEFT: 1px solid" align="right">&nbsp;0,00</td>
    <td style="BORDER-LEFT: 1px solid" align="right">&nbsp;30</td>
    <td style="BORDER-LEFT: 1px solid" align="right">&nbsp;0</td>
    <td style="BORDER-LEFT: 1px solid" align="right">&nbsp;3</td>
    <td style="BORDER-LEFT: 1px solid" align="right">&nbsp;</td>
    <td style="BORDER-LEFT: 1px solid; BORDER-RIGHT: 1px solid" align="right">&nbsp;</td>
    <td style="BORDER-LEFT: 1px solid; BORDER-RIGHT: 1px solid" align="right">&nbsp;</td>
    <td style="BORDER-LEFT: 1px solid;BORDER-RIGHT: 1px solid;" align="right">&nbsp;5000</td>
</tr>

1 个答案:

答案 0 :(得分:0)

您可以使用replace函数。

TEXT = '<br>test&nbsp;&nbsp;test'
TEXT = TEXT.replace('<br>', '').replace('&nbsp;', '')