Nokogiri help

  • Thread starter R.. Kumar 1.9.1 OSX
  • Start date
R

R.. Kumar 1.9.1 OSX

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
 
H

Hassan Schroeder

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").
doc.css('p.nextprev').each do |link|

doc.css('p.nextprev/a').each do |a|
...
 
M

Mike Dalessio

[Note: parts of this message were removed to make it a legal post.]

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

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
# => [#<Nokogiri::XML::Attr:0x3fa59149a0cc name="href" value="
http://www.reddit.com/r/programming/?count=25&after=t3_cygpc">]



 
R

R.. Kumar 1.9.1 OSX

Mike said:
On Sun, Aug 8, 2010 at 11:35 AM, R.. Kumar 1.9.1 OSX
<[email protected]


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
# => [#<Nokogiri::XML::Attr:0x3fa59149a0cc name="href" value="
http://www.reddit.com/r/programming/?count=25&after=t3_cygpc">]

thanks a lot to both answers.

1) Is there any doc that has advanced info or examples, the tutorial had
simple examples.

2) Also, is Nokogiri the right tool for this job, or is Scrubyt more
suitable.

reg
rahul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top