Created
April 19, 2016 03:19
-
-
Save bchase/22f3d91e2f49988cfd0e6e68607dfa29 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'nokogiri' | |
| require 'pry' | |
| html_str = <<-HTML | |
| <html> | |
| <body> | |
| <p> | |
| <a href="example.com"><span>click here</span></a> | |
| </p> | |
| <ul> | |
| <li class="odd">foo</li> | |
| <li class="even">bar</li> | |
| <li class="odd active">baz</li> | |
| </ul> | |
| </body> | |
| </html> | |
| HTML | |
| ### PARSE ### | |
| doc = Nokogiri::HTML(html_str) # Nokogiri::XML(xml_str) | |
| ### SEARCH ### | |
| doc.xpath('//li').map &:text | |
| # => ["foo", "bar", "baz"] | |
| doc.css("li").map &:text | |
| # => ["foo", "bar", "baz"] | |
| doc.css("li.even").count | |
| # => 1 | |
| a = doc.css('a[href="example.com"]').first | |
| # => #(Element:0x16f3d48 { | |
| # name = "a", | |
| # attributes = [ #(Attr:0x16f2b8c { name = "href", value = "example.com" })], | |
| # children = [ #(Element:0x145ec2c { name = "span", children = [ #(Text "click here")] })] | |
| # }) | |
| ### PARENT / CHILDREN ### | |
| a.children.first.name | |
| # => "span" | |
| a.children.first.text | |
| # => "click here" | |
| a.parent.name | |
| # => "p" | |
| ### ATTRIBUTES ### | |
| a['href'] | |
| # => "example.com" | |
| a.attributes | |
| # => {"href"=>#(Attr:0x1494700 { name = "href", value = "example.com" })} | |
| a.attributes['href'] | |
| # => #(Attr:0x1494700 { name = "href", value = "example.com" }) | |
| a.attributes['href'].name | |
| # => "href" | |
| a.attributes['href'].value | |
| # => "example.com" | |
| binding.pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment