Alan
Alan

Reputation: 479

How to find text on a page using Nokogiri

I am trying to find the best way to find a word on a page using Nokogiri.

I have a page which has the following text.

<p>Modelo: ABC123-A</p>

I would like to find the "Modelo:" text, and then get the model number after it.

I have had a look around but can't seem to find. So, I thought I would post on here and see if anyone with experience of Nokogiri could shed some light on this for me.

Upvotes: 1

Views: 2104

Answers (2)

kiddorails
kiddorails

Reputation: 13014

Use p:contains selector and get the matching p nodes.

doc = Nokogiri::HTML('<html><body><p>Modelo: ABC123-A</p><br/><p>Nothing here</p><p>Modelo: 4321</p></body></html>')
doc.css('p:contains("Modelo")').map { |x| x.text.split(': ').last }
#=> ["ABC123-A", "4321"]

Upvotes: 1

Mat
Mat

Reputation: 2184

A simple example:

doc = Nokogiri::HTML('<html><body><p>Modelo: ABC123-A</p></body></html>')
doc.css('p').first.content  # => Modelo: ABC123-A
str.split( ': ' )[-1]  # => ABC123-A

You could also try Oga, it's lighter than Nokogiri.

Upvotes: 1

Related Questions