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.

有帮助吗?

解决方案

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"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top