Skip to content

Instantly share code, notes, and snippets.

@donpdonp
Created February 13, 2011 20:31
Show Gist options
  • Save donpdonp/825089 to your computer and use it in GitHub Desktop.
Save donpdonp/825089 to your computer and use it in GitHub Desktop.
## convert somedomain.com to newhost.com
require 'rubygems'
require 'nokogiri'
require 'uri'
# change this to File.open
html_text = "<a href='http://somedomain.com/something/'>
<a href='http://somedomain.com/something/else'>"
html = Nokogiri::HTML(html_text)
anchors = html.css("a")
anchors.each do |anchor|
url = URI.parse(anchor['href'])
url.host = "newhost.com"
anchor['href'] = url.to_s
end
puts html
## output
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<a href="http://newhost.com/something/"></a>
<a href="http://newhost.com/something/else"></a>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment