Last active
August 29, 2015 14:06
-
-
Save aviddiviner/f37fc1266ef779e06fe1 to your computer and use it in GitHub Desktop.
Command line Nokogiri
This file contains 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 | |
# | |
# 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