This magical Ruby script downloads a couple of XML files from a website, but the files don’t have an extension. I’d like to add the prefix .xml to every file it downloads.
This is where I am right now and it won’t work:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
URL = 'localhost'
extension = '.xml'
Nokogiri::HTML(open(URL)).xpath("//a/@href").each do |src|
src = File.join(extension).last
File.open(File.basename(src),'wb') do |f|
f.write(open(src).read)
end
puts "Done with: #{URL}"
end
Any ideas on how to make this work?
I am assuming that the
hrefattributes in your page’s links contain absolute paths. With that in mind, this should work.