Skip to content

Instantly share code, notes, and snippets.

@miyamuko
Created July 29, 2010 07:25
Show Gist options
  • Select an option

  • Save miyamuko/497494 to your computer and use it in GitHub Desktop.

Select an option

Save miyamuko/497494 to your computer and use it in GitHub Desktop.
Ruby + htmlfile(WIN32OLE) + JavaScript-XPath で XPath
# Ruby + htmlfile + JavaScript-XPath で XPath
#
# Example:
#
# > ruby htmlfile-xpath.rb http://gist.github.com/gists "//div[@class='info']/span/a"
# gist: 497491
# gist: 497481
# gist: 497469
# :
#
# See:
# http://d.hatena.ne.jp/amachang/20071112/1194856493
# http://d.hatena.ne.jp/mobitan/20090829/1251542456#20090829f10
require "open-uri"
require "win32ole"
$jsxpath_uri = "http://svn.coderepos.org/share/lang/javascript/javascript-xpath/trunk/release/javascript-xpath-latest-cmp.js"
if ARGV.size != 2
$stderr.puts "Usage: uri xpath"
exit 1
end
uri, xpath = *ARGV
dom = WIN32OLE.new("htmlfile")
dom.Write(open(uri){|f| f.read })
dom.parentWindow.eval(open($jsxpath_uri){|f| f.read })
items = dom.evaluate(xpath, dom, nil, 7, nil)
len = items.snapshotLength
(0...len).each do |i|
item = items.snapshotItem(i)
puts item.innerHTML
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment