I need to write image parser from some website which will take images, some other info and save it to my local folder. So let's say we have image at this url : https://i.stack.imgur.com/MiqEv.jpg (this is someone's SO avatar)
So I want to to save it to local folder. Let's say to "~/test/image.png"
I found this link
And I tried this in my terminal:
rails console
require 'open-uri'
open('~/test/image.jpg', 'wb') do
|file| file << open('https://i.stack.imgur.com/MiqEv.jpg').read
end
As you can see my home/test folder is empty
And I got this output from console
#<File:~/test/image.jpg (closed)>
What do I do?
Also I tried this:
require 'open-uri'
download = open('https://i.stack.imgur.com/MiqEv.jpg')
IO.copy_stream(download, '~/test/image.jpg')
And got this output:
=> #https://i.stack.imgur.com/MiqEv.jpg>, @meta={"date"=>"Fri, 06 May 2016 11:58:05 GMT", "content-type"=>"image/jpeg", "content-length"=>"4276", "connection"=>"keep-alive", "set-cookie"=>"__cfduid=d7f982c0742bf40e58d626659c65a88841462535885; expires=Sat, 06-May-17 11:58:05 GMT; path=/; domain=.imgur.com; HttpOnly", "cache-control"=>"public, max-age=315360000", "etag"=>"\"b75caf18a116034fc3541978de7bac5b\"", "expires"=>"Mon, 04 May 2026 11:58:05 GMT", "last-modified"=>"Thu, 28 Mar 2013 15:05:35 GMT", "x-amz-version-id"=>"TP7cpPcf0jWeW2t1gUz66VXYlevddAYh", "cf-cache-status"=>"HIT", "vary"=>"Accept-Encoding", "server"=>"cloudflare-nginx", "cf-ray"=>"29ec4221fdbf267e-FRA"}, @metas={"date"=>["Fri, 06 May 2016 11:58:05 GMT"], "content-type"=>["image/jpeg"], "content-length"=>["4276"], "connection"=>["keep-alive"], "set-cookie"=>["__cfduid=d7f982c0742bf40e58d626659c65a88841462535885; expires=Sat, 06-May-17 11:58:05 GMT; path=/; domain=.imgur.com; HttpOnly"], "cache-control"=>["public, max-age=315360000"], "etag"=>["\"b75caf18a116034fc3541978de7bac5b\""], "expires"=>["Mon, 04 May 2026 11:58:05 GMT"], "last-modified"=>["Thu, 28 Mar 2013 15:05:35 GMT"], "x-amz-version-id"=>["TP7cpPcf0jWeW2t1gUz66VXYlevddAYh"], "cf-cache-status"=>["HIT"], "vary"=>["Accept-Encoding"], "server"=>["cloudflare-nginx"], "cf-ray"=>["29ec4221fdbf267e-FRA"]}, @status=["200", "OK"]> 2.3.0 :244 > IO.copy_stream(download, '~/test/image.jpg') => 4276
But my folder is still empty. What do I do??
~
with/home/yourusername/
– Mladen JablanovićErrno::ENOENT: No such file or directory @ rb_sysopen - home/jonstark/test/image.jpg
– user2950593home/jonstark
and it gave me an error. Then I wrote/home/jonstark/
with/
beforehome
and it worked! write it as an answer! i will accept it=) – user2950593