Skip to content

Instantly share code, notes, and snippets.

@NigoroJr
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save NigoroJr/925edc61b512966b017c to your computer and use it in GitHub Desktop.

Select an option

Save NigoroJr/925edc61b512966b017c to your computer and use it in GitHub Desktop.
Sample program for using xpath with nokogiri
#!/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