Created
September 8, 2011 08:44
-
-
Save moro/1202952 to your computer and use it in GitHub Desktop.
Nokogiri+grep
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 'optparse' | |
$optval = {} | |
opt = OptionParser.new | |
opt.on('-a', '--attr=ATTR', 'display attribute value') {|v| $optval[:attr] = v } | |
opt.on('-i', '--inspect', 'output #inspect instead of #to_s') { $optval[:inspect] = true } | |
opt.parse!(ARGV) | |
pattern, path, = ARGV | |
Nokogiri::HTML(path ? File.open(path) : $stdin).css(pattern).each do |elem| | |
if attr = $optval[:attr] | |
elem = elem.attribute(attr) | |
end | |
puts $optval[:inspect] ? elem.inspect : elemn.content | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
出力はこんな感じ(日本から)