HTML/XML manipulation in 6 lines of Ruby
There’s a post up on HackerNews about how awesome HTML parsing and manipulation in Node.js + jQuery is. It also mentions in passing hpricot, and asserts “The challenge with using these libraries is that they all have their own quirks that can make working with HTML, CSS and Javascript challenging.”
Well, nokogiri queries with CSS3 selectors or XPath if you need to get crazy, so beats the hell out of me what they’re talking about. Anyway, lets fix their post for them:
require 'nokogiri'
require 'open-uri'
page = Nokogiri::HTML(open("http://blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs))
ruby_xml_link = page.css("a").select{ |a| a.text == "hpricot" }.first
ruby_xml_link.attributes['href'] = "http://nokogiri.org"
ruby_xml_link.content = "nokogiri (鋸)"
puts page
-
knowtheory posted this