grabbing text between two elements in nokogiri?

2019-04-10 11:03发布

<body>
 <div>some text</div>
 I NEED THIS TEXT ONLY
 <div>some text</div>
 more text here
 <div>some text</div>
 one more text here
 <div>some text</div>
</body>

How?

4条回答
Bombasti
2楼-- · 2019-04-10 11:37

This XPath 1.0:

/body/text()[preceding-sibling::*[1][self::div]]
            [following-sibling::*[1][self::div]][1]

Also:

/body/text()[normalize-space()][1]
查看更多
爷的心禁止访问
3楼-- · 2019-04-10 11:39

I don't have nokogiri, but here's an alternative using just basic string manipulation.

html=<<EOF
<body>
 <div>some text</div>
 I NEED THIS TEXT ONLY
 <div>some text</div>
 more text here
 <div>some text</div>
 one more text here
 <div>some text</div>
</body>
EOF
p html.split(/<\/*body>/)[1].split(/<\/div>/)[1].split(/<div>/)[0]
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-04-10 11:54

Use:

/*/div[1]/following-sibling::text()[1]

This selects the first text-node sibling of the first div child of the top element of the document.

查看更多
做个烂人
5楼-- · 2019-04-10 11:54

this returns the first text node within body between two div elements:

/body/text()[
     ./preceding::element()[1][local-name()="div"] and 
     ./following::element()[1][local-name()="div"]
][1]

should return

I NEED THIS TEXT ONLY
查看更多
登录 后发表回答