【发布时间】:2013-11-13 15:35:46
【问题描述】:
我在这方面完全是新手,但是两天来一直试图用 BeautifulSoup 解析一些 HTML,但没有任何真正的好结果。 有一次我设法删除所有 HTML 并保留我想要的文本,但在我正在解析的整个表上只得到 1 个结果,而另一次我得到了我想要的一切,但似乎无法全部删除HTML。
from bs4 import BeautifulSoup
soup = BeautifulSoup (open("PlusGrosCAVerif.htm"))
raisonsociale = soup.find('td', {'class' : 'verif_col1'})
for noms in raisonsociale:
listenom = raisonsociale.get_text()
print(listenom)
HTML 看起来像这样:
<table id="verif_hitparade_donnees">
<tr id="verif_meslistes_thead">
<th class="verif_col1">Raison sociale</th>
<th class="verif_col2">CP</th>
<th class="verif_col3">Ville</th>
<th class="verif_col5">C.A.</th>
</tr>
<tr class="verif_result_tr_opaq2">
<td class="verif_col1"><a href="/societe/M-H-C-S-509553459/">M H C S</a></td>
<td class="verif_col2"><a href="/societe/M-H-C-S-509553459/">51200</a></td>
<td class="verif_col3"><a href="/societe/M-H-C-S-509553459/">EPERNAY</a></td>
<td class="verif_col5"><a href="/societe/M-H-C-S-509553459/">1 472 239 977 €</a></td>
</tr>
<tr class="verif_result_tr_opaq">
<td class="verif_col1"><a href="/societe/VIVESCIA-302715966/">VIVESCIA</a></td>
<td class="verif_col2"><a href="/societe/VIVESCIA-302715966/">51100</a></td>
<td class="verif_col3"><a href="/societe/VIVESCIA-302715966/">REIMS</a></td>
<td class="verif_col5"><a href="/societe/VIVESCIA-302715966/">1 277 349 946 €</a></td>
</tr>
<tr class="verif_result_tr_opaq2">
<td class="verif_col1"><a href="/societe/SOC-COOP-APPROVISIONNEMENT-PARIS-EST-301986154/">SOC COOP APPROVISIONNEMENT PARIS EST</a></td>
<td class="verif_col2"><a href="/societe/SOC-COOP-APPROVISIONNEMENT-PARIS-EST-301986154/">51520</a></td>
<td class="verif_col3"><a href="/societe/SOC-COOP-APPROVISIONNEMENT-PARIS-EST-301986154/">SAINT MARTIN SUR LE PRE</a></td>
<td class="verif_col5"><a href="/societe/SOC-COOP-APPROVISIONNEMENT-PARIS-EST-301986154/">1 249 176 407 €</a></td>
</tr>
<tr class="verif_result_tr_opaq">
<td class="verif_col1"><a href="/societe/ARCELORMITTAL-DISTRI-SOLUTIONS-FRANCE-469500961/">ARCELORMITTAL DISTRI SOLUTIONS FRANCE</a></td>
<td class="verif_col2"><a href="/societe/ARCELORMITTAL-DISTRI-SOLUTIONS-FRANCE-469500961/">51100</a></td>
<td class="verif_col3"><a href="/societe/ARCELORMITTAL-DISTRI-SOLUTIONS-FRANCE-469500961/">REIMS</a></td>
<td class="verif_col5"><a href="/societe/ARCELORMITTAL-DISTRI-SOLUTIONS-FRANCE-469500961/">586 085 818 €</a></td>
</tr>
<tr class="verif_result_tr_opaq2">
<td class="verif_col1"><a href="/societe/SEVEAL-757803689/">SEVEAL</a></td>
<td class="verif_col2"><a href="/societe/SEVEAL-757803689/">51100</a></td>
<td class="verif_col3"><a href="/societe/SEVEAL-757803689/">REIMS</a></td>
<td class="verif_col5"><a href="/societe/SEVEAL-757803689/">480 141 491 €</a></td>
</tr>
<tr class="verif_result_tr_opaq">
<td class="verif_col1"><a href="/societe/ACOLYANCE-381960491/">ACOLYANCE</a></td>
<td class="verif_col2"><a href="/societe/ACOLYANCE-381960491/">51100</a></td>
<td class="verif_col3"><a href="/societe/ACOLYANCE-381960491/">REIMS</a></td>
<td class="verif_col5"><a href="/societe/ACOLYANCE-381960491/">462 996 287 €</a></td>
</tr>
...持续了很长一段时间。
我想做的是解析 td 类“verif_col”1、2、3 和 5,所以我可以把它们放在一个 CSV 文件中,所以我首先尝试获取名称 (verif_col1),去掉它们的任何html 左右。使用上面的代码,我只得到名字(MHCS),然后脚本停止。
我已经尝试过 findAll,但我无法使用 get_text() 方法让它工作。我想过 findNext() 之类的,但没有结果。
对于一个迷失和无知的新手有什么想法吗?
非常感谢
【问题讨论】:
标签: python html beautifulsoup