Skip to content
boooz edited this page May 17, 2012 · 3 revisions

Skyscraper nodes has following methods helping with traversing:

first(selector)

Returns first node matches provided selector

>> Skyscraper::fetch("http://rubyonrails.org").first("a")
# => #<Skyscraper::Node::Base:0xb88c524 @element=#<Nokogiri::XML::Element:0x5c4629c name="a" attributes=[#<Nokogiri::XML::Attr:0x5b0158a name="href" value="/">] children=[#<Nokogiri::XML::Text:0x5b01332 "Overview">] 

find(selector)

Returns an array of nodes, matches provided selector

>> Skyscraper::fetch("http://rubyonrails.org").find("#Nav a").map(&:text)
# => ["Overview", "Download", "Deploy", "Bugs/Patches", "Screencasts", "Documentation", "Ecosystem", "Community", "Blog"]

children(selector = nil)

Returns an array of node childrens, matching selector if provider.

>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav").children.map(&:html)
# => ["Overview", "Download", "Deploy", "Bugs/Patches", "Screencasts", "Documentation", "Ecosystem", "Community", "Blog"]
>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav").children("a:first-child").map(&:html)
# => ["Overview"]

Notice!

It doesn't returns children of childrens.

parent

Returns node parent

>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav").parent.id
# => "Header"

parents(selector = nil)

Returns node parents, matching selector if provided.

>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav").parents.map(&:tag)
# => ["div", "div", "body", "html"]
>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav").parents("div").map(&:tag)
# => ["div", "div"]

siblings

Returns an array of node siblings

>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav a").siblings.map(&:text)
# => ["Download", "Deploy", "Bugs/Patches", "Screencasts", "Documentation", "Ecosystem", "Community", "Blog"]

follow

If an elemenent have href attribute you can fetch page on which it indicates.

>> Skyscraper::fetch("http://rubyonrails.org").find("#Nav a")[1].follow.first("title").text
# => "Ruby on Rails: Download"

Clone this wiki locally