Last active
August 29, 2015 14:22
-
-
Save NigoroJr/925edc61b512966b017c to your computer and use it in GitHub Desktop.
Sample program for using xpath with nokogiri
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 'robotex' | |
| require 'nokogiri' | |
| require 'open-uri' | |
| url = 'http://www.cplusplus.com/reference/ios/basic_ios/' | |
| # Fake User Agent | |
| ua = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1' | |
| robotex = Robotex.new | |
| puts robotex.allowed?(url) | |
| charset = 'utf8' | |
| html = open(url, 'User-Agent' => ua) | |
| doc = Nokogiri::HTML::parse(html) | |
| # html = open(url, 'User-Agent' => ua) do |f| | |
| # charset = f.charset | |
| # f.read | |
| # end | |
| # doc = Nokogiri::HTML::parse(html, nil, charset) | |
| puts doc.xpath('//*[@id="types"]/dl[4]') | |
| # Can also do something like this | |
| # puts doc.xpath('//*[@id="types"]/dl[*]') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment