Question

I am trying to scrape some content and parse it using Nokogiri! Got struck now since i am trying to get some text content which is not enclosed with in any kind of tags. Just text and some of it in tags with out any class or id to it.

Can i find content by searching just the content/text starting and ending of it and get all of it in between?

<body>
text <br/>
<ul>
<li>some more text </li>
</body>

CSS selectors or Xpath, any solution would be great.

Was it helpful?

Solution

require "nokogiri"

Nokogiri::HTML.parse(<<_).css("body").children.first.text
<body>
text <br/>
<ul>
<li>some more text </li>
</body>
_
# => "\ntext "


Nokogiri::HTML.parse(<<_).css("body").children.first.text.strip
<body>
text <br/>
<ul>
<li>some more text </li>
</body>
_
# => "text"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top