如何在DIV标签内和使用XPath的第一个H3标签之前选择P标签?

时间:2017-06-25 07:28:33

标签: xpath scrapy

这是我的HTML:

<div class="main">
<p>Abcd</p>
<p>Abcd</p>
<h3>Head 3.1</h3>
<p>Abcd</p>
<h3>Head 3.2</h3>
</div>

我需要在<p>标记内以及使用XPath的第一个<div>标记之前选择<h3>个标记。怎么做?

1 个答案:

答案 0 :(得分:3)

您可以在xpath中使用[not(preceding-sibling::h3)]语句来仅获取上面没有h3个节点的节点:

> response.xpath("//div/p[not(preceding-sibling::h3)]").extract()
< [u'<p>Abcd</p>', u'<p>Abcd</p>']
相关问题