2011-03-21 16 views
7

की सामग्री मैं इसमाणिक Nokogiri xpath प्राप्त नोड

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

    puts html # how get content of a node 
end 

की तरह कोड है और मेरे सवाल यह है कि नोड के सामग्री प्राप्त करने क्योंकि अब मैं इस तरह sth मिला है।

<li class="stat"> 

उत्तर

11

देखें इस Nokogiri के लिए README file में सार उदाहरण एक तरह से सीएसएस, XPath या एक संकर का उपयोग कर यह करने के लिए दिखा रहा है:

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 या html.text

Node documentation

+1

लिंक मर चुका है, शायद आप यहां इंगित करना चाहते हैं: http://www.rubydoc.info/gems/nokogiri/Nokogiri/XML/Node अब? –

संबंधित मुद्दे