How do I open a web page and write it to a file in ruby?
require 'open-uri'
open("file_to_write.html", "wb") do |file|
URI.open("http://www.example.com/") do |uri|
file.write(uri.read)
end
end
Note: In Ruby < 2.5 you must use open(url)
instead of URI.open(url)
. See https://bugs.ruby-lang.org/issues/15893