Skip to content

Instantly share code, notes, and snippets.

@aviddiviner
Last active August 29, 2015 14:06
Show Gist options
  • Save aviddiviner/f37fc1266ef779e06fe1 to your computer and use it in GitHub Desktop.
Save aviddiviner/f37fc1266ef779e06fe1 to your computer and use it in GitHub Desktop.
Command line Nokogiri
#!/usr/bin/env ruby
#
# A little wrapper around the Ruby Nokogiri gem for easy markup scanning.
#
# Usage: cat example.html | noko 'body .header[2]'
# Usage: curl -L http://google.com | noko 'table a'
# Usage: noko 'css selector'
# (Type in some lines, ctrl-d when done.)
#
require 'nokogiri'
doc = Nokogiri::HTML(STDIN.read)
nodes = doc.css(ARGV.first)
nodes.each do |node|
if node.is_a?(Nokogiri::XML::Attr)
puts node.text
else # Nokogiri::XML::{Element, Text}
puts node.to_xml
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment