我是xpath的新手,所以请耐心听我说。目前,我正在使用scrapy从一些网页上抓取一些内容,内容看起来像这样:
<td colspan="3" valign="top" class="regular">Landsize: 84,000sq with an extensive shoreline 750m<br />
<br />
Call Or Email for more info<br />
. Full-length Olympicpool,children pool,jacuzzi<br />
\' Landscapesdkey bridges<br />
. 2 tennis courts<br />
. water features True seafront development with iconic design by architect Daniel Libeskind<br />
lconic residential, located less than\' 150 metres from the shoreline<br />
<br />
opposite the future integrated resort on sentosa Island.<br />
A part of keppel Bay world calss water front precinct with luxury homes.<br />
<br />
Call or email for more info </td>具体地说,我使用了以下hxs.select('//tr[contains(td,"Description")]/following-sibling::tr[1]/td/text()').extract()
但是,这样做会将结果项分解到一个列表中,因为内容是由<br>分隔的。如果我从xpath中排除text(),<td>元素将包含在结果字符串中,这是不可取的。
在xpath中有没有一种方法可以确保我得到的字符串是上面所示的所有内容,但没有td标记?我希望我不需要通过<br/>手动加入列表
发布于 2011-11-11 13:58:19
从你对埃文的正确答案的评论来看,你想跳过。
在这种情况下,请尝试:
normalize-space(//tr[contains(td,"Description")]/following-sibling::tr[1]/td)便笺
normalize-space()的参数选择了多个节点,则此函数将返回仅处理第一个选定节点的结果。发布于 2011-11-11 02:55:12
尝试将表达式包装在对string()的调用中,该调用返回节点的字符串值,该值是节点的后代文本节点的所有字符串值的串联。
string(//tr[contains(td,"Description")]/following-sibling::tr[1]/td)发布于 2011-11-11 03:17:14
您可能会发现HTML Agility Pack对于解析web页面很有用。
https://stackoverflow.com/questions/8084484
复制相似问题