I have html code:
<div class="text-conent" itemprop="articleBody" id="baiviet-container">
<div class="baiviet-bailienquan pink-box-bg-light">
<p><a href="...">title a</a></p>
</div>
<p><a href="...">title b</a></p>
<p><a href="...">title c</a></p>
</div>
I used Xpath to get a/@href
like:
.//div[id="baiviet-container"]/a/@href
I want get only:
<p><a href="...">title b</a></p>
<p><a href="...">title c</a></p>
and not get any a/@href
in
<div class="baiviet-bailienquan pink-box-bg-light">...</div>
How can i do it?
Thanks you very much :)
答案 0 :(得分:0)
你的XPath几乎可以工作;它在p
和div
之间的步骤中仅丢失a
,并且@
属性也缺少id
:
.//div[@id="baiviet-container"]/p/a/@href
快速测试:http://www.xpathtester.com/xpath/6b09187232acd7e5f6b7c3a617bd1e05
无需担心错误地从内部@href
获取div
,因为XPath已经使用div[@id="baiviet-container"]/p
中提到的子轴。因此,它将考虑给定的p
元素,仅当它是div
的直接子且id
属性等于"baiviet-container"
。