How to parse HTML using nokogiri if the required content doesn't have a class or id? [closed]

StackOverflow https://stackoverflow.com/questions/22599583

Вопрос

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