9

I have code like this:

@doc = Nokogiri::HTML(open(url)
@doc.xpath(query).each do |html|

  puts html # how get content of a node
end

How do I get the content of the node instead of something like this:

<li class="stat">
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
John
  • 1,350
  • 5
  • 27
  • 49

2 Answers2

13

This is the Synopsis example in the README file for Nokogiri showing one way to do it using CSS, XPath or a hybrid:

require 'nokogiri'
require 'open-uri'

# Get a Nokogiri::HTML:Document for the page we’re interested in...

doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove'))

# Do funky things with it using Nokogiri::XML::Node methods...

####
# Search for nodes by css
doc.css('h3.r a.l').each do |link|
  puts link.content
end

####
# Search for nodes by xpath
doc.xpath('//h3/a[@class="l"]').each do |link|
  puts link.content
end

####
# Or mix and match.
doc.search('h3.r a.l', '//h3/a[@class="l"]').each do |link|
  puts link.content
end
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
5

See html.content or html.text.

See the Node documentation for more information.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Lee Jarvis
  • 16,031
  • 4
  • 38
  • 40
  • 1
    The link is dead, maybe you want to point at : http://www.rubydoc.info/gems/nokogiri/Nokogiri/XML/Node now ? – Marc-Andre Jan 19 '17 at 13:55
  • I fixed the broken link. When linking to off-site documentation, it's really important to include a summary of the most important point you're making, along with the reference and attribution to the document. Links rot then break and when they do they're useless to anyone. – the Tin Man Jul 18 '19 at 02:34