-
Notifications
You must be signed in to change notification settings - Fork 1
Reading attributes
boooz edited this page May 17, 2012
·
2 revisions
You can get any HTML attribute calling method directly on node element
>> Skyscraper::fetch("http://rubyonrails.org").first(".message").class
# => "message"
>> Skyscraper::fetch("http://rubyonrails.org").first(".message").id
# => ""
>> Skyscraper::fetch("http://rubyonrails.org").first("#Nav").id
# => "Nav"
>> Skyscraper::fetch("http://rubyonrails.org").first("img").src
# => "/images/rails.png"
>> Skyscraper::fetch("http://rubyonrails.org").first("img").width
# => "87"Returns text content of an element - it is also striping html tags (to get html use html method)
>> Skyscraper::fetch("http://rubyonrails.org").first("p").text
# => "Screencasts & presentations"Returns HTML code of an element
>> Skyscraper::fetch("http://rubyonrails.org").first(".message").html
# => "\r\n <img src=\"/images/rails.png\" width=\"87\" height=\"112\" style=\"margin-right: 10px\" alt=\"Rails\"><img src=\"/images/headers/overview.gif\" width=\"603\" height=\"112\" alt=\"Web development that doesn't hurt\">"Returns HTML tag name of an element
>> Skyscraper::fetch("http://rubyonrails.org").first(".message").tag
# => "div"Returns Nokogiri::XML::Element node
>> Skyscraper::fetch("http://rubyonrails.org").first(".message").element
# => #<Nokogiri::XML::Element:0x51d137a name="div" attributes=[#<Nokogiri::XML::Attr:0x51d117c name="class" value="message">] children=[#<Nokogiri::XML::Text:0x51d0cae "\r\n ">, #<Nokogiri::XML::Element:0x51d0be6 name="img" attributes=[#<Nokogiri::XML::Attr:0x50bc7aa name="src" value="/images/rails.png">, #<Nokogiri::XML::Attr:0x50bc778 name="width" value="87">, #<Nokogiri::XML::Attr:0x50bc76e name="height" value="112">, #<Nokogiri::XML::Attr:0x50bc75a name="style" value="margin-right: 10px">, #<Nokogiri::XML::Attr:0x50bc750 name="alt" value="Rails">]>, #<Nokogiri::XML::Element:0x50bbd46 name="img" attributes=[#<Nokogiri::XML::Attr:0x50bbd0a name="src" value="/images/headers/overview.gif">, #<Nokogiri::XML::Attr:0x50bbd00 name="width" value="603">, #<Nokogiri::XML::Attr:0x50bbcf6 name="height" value="112">, #<Nokogiri::XML::Attr:0x50bbcec name="alt" value="Web development that doesn't hurt">]>]>Returns path do downloaded file, it accepts options hash, which can have path and file_name attributes
>> Skyscraper::fetch("http://rubyonrails.org").first(".message img").download
=> "/tmp/test/my_path_from_config/rails.png"Returns an absolute path for href attribute
>> Skyscraper::fetch("http://rubyonrails.org").find("a")[1].uri
=> "http://rubyonrails.org/download"