【发布时间】:2019-07-08 10:24:51
【问题描述】:
我正在尝试从该网站获取信息 https://www.realtypro.co.za/property_detail.php?ref=1736
我有这张桌子,我想从中获取卧室的数量
<div class="panel panel-primary">
<div class="panel-heading">Property Details</div>
<div class="panel-body">
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="table table-striped table-condensed table-tweak">
<tbody><tr>
<td class="xh-highlight">3</td><td style="width: 140px" class="">Bedrooms</td>
</tr>
<tr>
<td>Bathrooms</td>
<td>3</td>
</tr>
我正在使用这个 xpath 表达式:
bedrooms = response.xpath("//div[@class='panel panel-primary']/div[@class='panel-body']/table[@class='table table-striped table-condensed table-tweak']/tbody/tr[1]/td[2]/text()").extract_first()
但是,我只得到“无”作为输出。
我尝试了几种组合,但我只得到 None 作为输出。关于我做错了什么有什么建议吗?
提前致谢!
【问题讨论】:
-
你需要第二个
tr。 -
@Utkanos 即使我将它转换为 tr[2]/td[2] 我仍然得到 None 作为输出
-
在你的问题中你说你需要浴室的数量;你的意思是
Bedrooms的数量是3的输出吗? -
另一种方法
.xpath("//*[starts-with(@class,'table')]//tr[contains(.,'Bedrooms')]/td/text()").get() -
您在上面粘贴的元素与该链接中可用的元素的顺序不同。在链接中,卧室的数量在后面,但在您提供的 html 数字中,在前面。
标签: html parsing xpath web-scraping web-crawler