如何使用 xpath 从第一个 td 中抓取文本



有人知道如何从第一个TD而不是下一个TD中抓取文本吗?如果没有值,则将其设为 0:

<tr>
<td style="width:28%;">
2 plantas··
&nbsp;
</td>
<td style="width:28%;">
300m² terreno

&nbsp;
</td>
</tr>

在上图中,我的代码(下图(也抓取了下一个 td,这是一个空白,但我想抓住写着"300m2 terreno"的那个:

terreno=tree.xpath('//td[contains(text(),"planta")]/following-sibling::td/text()')
terreno2=[item.strip() for item in terreno]
terreno3=[]
for casa in terreno2:
if len(casa)<1: continue
terreno3.append(float(casa.split('m²')[0]))

我正在输出这个:

['300m² terreno', '', '', '', '', '', '315m² terreno', '', '', '', '', ''....]

这是我来源的链接: https://www.avisosdeocasion.com/Resultados-Inmuebles.aspx?n=venta-casas-nuevo-leon&PlazaBusqueda=2&Plaza=2

使用此 xpath :

//td[contains(text(),"planta")]/following-sibling::td[1]/text()
#                                                     ^
limit to the fisrt 'td'

最新更新