Skip to content

Instantly share code, notes, and snippets.

@donnior
Created December 15, 2011 01:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donnior/1479458 to your computer and use it in GitHub Desktop.
Save donnior/1479458 to your computer and use it in GitHub Desktop.
Html Parse with Nokogiri
require 'rubygems'
require 'nokogiri'
require 'open-uri'
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.854.0 Safari/535.2"
html = "http://www.somedomain.com/somepage/"
@doc = Nokogiri::HTML(open(html, 'User-Agent' => user_agent), nil, "UTF-8")
@doc.xpath("//li[a[text()='Home']]/following-sibling::li").each do |link|
puts link.text
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment