Archive for March 2011

Download any page from web url

Friday, March 18, 2011 · Posted in

Here is a code in ruby to save any page from given web url.

if web_url.match("(.*?)//(.*?)/(.*?) ")
   site = $2
   url = "/" + $3
   ext = url.downcase.reverse[0..url.reverse.index("\.")]

   Net::HTTP.start("#{site}") { |http|
      resp = http.get("#{url}")
      open("filename-with-full-path", "wb") { |file|
          file.write(resp.body)
      }
   }
end


If you want to restrict on image download then,
add this code after 'ext' variable

if(ext.reverse.to_s.eql?("\.png") or ext.reverse.to_s.eql?("\.gif"))
   Net::HTTP.start("#{site}") { |http|
      resp = http.get("#{url}")
      open("filename-with-full-path", "wb") { |file|
          file.write(resp.body)
      }
   }
else
   #TODO Error Message
end

Powered by Blogger.