XPath recursive children selection
Asked Answered
R

2

9

I'm using scrapy to extract data from a web site, but I have a problem with the XPath selector, assuming i have this HTML code:

<div id="_parent">
    Hi!
    <p>I am a child!</p>
    <span class="someclass">I am a <b>span</b> child!</span>
</div>

what I get:

I am a child
I am a  child!

what I should get:

Hi!
I am a child!
I am a span child!

The XPath I am using is the following: .//div[@id="_parent"]//*/text() I know this is because is not a direct children of the #_parent div but how can I recursively get all the children?

Recalcitrate answered 17/9, 2013 at 21:24 Comment(0)
S
15

You can just use: .//div[@id="_parent"]//text() to fetch all text node children of the selected node. You can test it here.

Scape answered 18/9, 2013 at 2:19 Comment(0)
C
0

If you want all data of an element (so, all string nodes), you can also use

data(.//div[@id="_parent"])
Cormorant answered 18/9, 2013 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.