# Nokogiri help

**URL:** <https://www.ruby-forum.com/t/nokogiri-help/192930>\
**Category:** Ruby\
**Created:** [August 8, 2010, 5:35pm UTC](https://www.ruby-forum.com/t/nokogiri-help/192930 "2010-08-08T17:35:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [August 8, 2010, 5:35pm UTC](https://www.ruby-forum.com/t/nokogiri-help/192930/1 "2010-08-08T17:35:22Z")

</div>

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(‘[Reddit - Dive into anything](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

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [August 8, 2010, 5:54pm UTC](https://www.ruby-forum.com/t/nokogiri-help/192930/2 "2010-08-08T17:54:15Z")

</div>

On Sun, Aug 8, 2010 at 8:35 AM, R… Kumar 1.9.1 OSX  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”).

> doc.css(‘p.nextprev’).each do |link|

doc.css(‘p.nextprev/a’).each do |a|  
…

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [August 9, 2010, 4:50am UTC](https://www.ruby-forum.com/t/nokogiri-help/192930/3 "2010-08-09T04:50:25Z")

</div>

On Sun, Aug 8, 2010 at 11:35 AM, R… Kumar 1.9.1 OSX  
\<removed\_email\_address@domain.invalid

> wrote:

> 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
# => [#<Nokogiri::XML::Attr:0x3fa59149a0cc name="href" value="

```

[http://www.reddit.com/r/programming/?count=25&after=t3\_cygpc](http://www.reddit.com/r/programming/?count=25&after=t3_cygpc)"\>]

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [August 9, 2010, 6:02am UTC](https://www.ruby-forum.com/t/nokogiri-help/192930/4 "2010-08-09T06:02:30Z")

</div>

Mike D. wrote:

> On Sun, Aug 8, 2010 at 11:35 AM, R… Kumar 1.9.1 OSX  
> \<removed\_email\_address@domain.invalid
> 
> > wrote:
> 
> 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](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

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [August 12, 2010, 2:42am UTC](https://www.ruby-forum.com/t/nokogiri-help/192930/5 "2010-08-12T02:42:40Z")

</div>

On Mon, Aug 09, 2010 at 01:02:33PM +0900, R… Kumar 1.9.1 OSX wrote:

> > ```
> > 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](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.

Nokogiri follows the XPath 1.0 spec. That means any XPath knowledge you  
gain can be used with Nokogiri. I suggest starting here:

[http://www.w3schools.com/xpath/](http://www.w3schools.com/xpath/)

Once you’ve mastered that, then I would go here:

[xpath cover page](http://www.w3.org/TR/xpath/)

> 1. Also, is Nokogiri the right tool for this job, or is Scrubyt more  
> suitable.

Nokogiri will probably work well for you, but I am not an expert with  
Scrubyt. You may want to investigate.
