From: Mike Dalessio Date: 2010-08-09T11:49:43+09:00 Subject: Re: Nokogiri help --0015175cdbfc717616048d5b11c4 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Aug 8, 2010 at 11:35 AM, R.. Kumar 1.9.1 OSX wrote: > I am trying to access some particular children in a document. I find > myself having to loop (several levels) through children checking name == > "xxx". I am wondering whething there is more direct way of getting the > same. Here's a simple sample. > > I am trying to get the href of a class called prevnext (on browser it is > "next"). > > require 'nokogiri' > require 'open-uri' > > > doc = Nokogiri::HTML(open('http://www.reddit.com/r/programming/')) > nextlink = nil > doc.css('p.nextprev').each do |link| > link.children.each do |child| > if child.name == "a" > nextlink = child.attributes["href"].value > end > end > end > puts nextlink > > -------- > Can i not access a.href more directly in the doc.css line itself ? > thx, rahul > For advanced usage like capturing attribute values in a Nokogiri search, you should use Node#xpath: doc = Nokogiri::HTML(open('http://www.reddit.com/r/programming/')) puts doc.xpath("//p[@class='nextprev']/a/@href").inspect # => [#] > -- > Posted via http://www.ruby-forum.com/. > > --0015175cdbfc717616048d5b11c4--