2011-03-21 24 views
7

que tiene un código como ésterubí Nokogiri contenido XPath get del nodo

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

    puts html # how get content of a node 
end 

y mi pregunta es cómo conseguir que el contenido del nodo porque ahora tengo algo como esto.

<li class="stat"> 

Respuesta

11

Este es el ejemplo Sinopsis en el README file para Nokogiri mostrando una forma de hacerlo es usando CSS, XPath o un híbrido:

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 
5

html.content o html.text

Ver el Node documentation

+1

El enlace está muerto, quizás quieras apuntar a: http://www.rubydoc.info/gems/nokogiri/Nokogiri/XML/Node ahora? –