How to get images from a saved html page

181 Views Asked by At

I have a huge amount of saved HTML pages in my PC. I had parsed the the HTML page and got the image src. I need to store the images in every HTML page in a specific structure in separate directory. I tried out NET::HTTP.get but i am getting a error of Filename too long. Is there any way to do this ??

Below are the ways i tried out.

Method 1:

{
require 'open-uri'

def save_image(imgsrc)
    File.open("images/img1","w") do |f|
        asdf = open(imgsrc).read
        f.write(asdf)
    end
end
}

Method 2:

{
require 'NET::HTTP'

def save_image(imgsrc)
    File.open("images/img1","w") do |f|
        asdf = Net::HTTP.get_response(URI.parse(imgsrc)
        f.write(asdf)
    end
end
}


imgsrc => data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABxAHEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImK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xng+aRS1Ju77Hop1XTlzm/tRj/psv+NMbW9KQZbUrMAcn98v+Nefj4MaXuBa7uz/ANt+v6VMvwb0T+N7hue8uc/pRZhdnZt4o0JQSdWs8Dr++FVZPG/hqP72tWn4NmucHwd8Oj+CQj3arMfwl8MIBm03Y9SaVmGppN8RPCaZ/wCJ1b/gG/wqrN8U/CMR/wCQoG5x8sbf4UifC/wwj7vsK5rQj8CeHY02/wBnREe60WYe8Y0nxf8ACUef9Kmb/diNQN8ZvCwXIN2fYQ//AF66f/hDtAyD/Zlvx0+WnjwnoQwf7Mtzj/Yos+4e8cc3xs8NgfJbag59oR/jVeT43aSEzFpWouew8sD+td8vhrRkOV022/74FTLoemL0sLcf9sxRyhqeYt8bWfP2fw3eN6Fv/rZrV8J/EjU/EfiO306bQJbSCUMWmYnC4UkdvbFd+mm2UYwlpCB7IKmSCKNgUjRfooFCQWZJRRRVFCd6KWigBKKWjvQAlFLRQAmKKWigBKKWigBKWiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA//2Q==
2

There are 2 best solutions below

2
On BEST ANSWER

You already have the images, the one you posted(in the imgsrc variable) is

this image

You only need to decode it using base64 module, and save the result to a file.

To decode your image i've used this service.


To decode using Base64 you should use #strict_decode64 method:

$ cat testb64.rb

imgsrc='/9j/4AAQS... ...oooA//2Q==' #( snipped here your long variable, 
                                    #  removed "data:image/jpeg;base64," 
                                    #  from the beginning )
require 'base64'
print Base64.strict_decode64(imgsrc)

$ ruby testb64.rb >img.jpg

$ xxd -p img.jpg 
ffd8ffe000104a464946....

(valid JFIF header, viewable JPEG by Gwenview and Dolphin)
1
On

This should work:

require 'open-uri'

require 'base64'
require 'open-uri'

def save_image(imgsrc)
  File.open("images/img1", "wb") do |fo|
    fo.write(Base64.decode64(open(imgsrc).read))
  end
end

It will save to the file path "images/img1" so you'll want to create separate paths for each file otherwise they'll overwrite each one.

"wb" means to open the output file using binary mode, which avoids line-end translations appropriate for your OS. Without b, Ruby will look for "\r" and "\n" and either remove or add them as necessary for a text file, which will corrupt a binary file. b avoids that step. This is documented in the IO.new description.

You can't pass

imgsrc => data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABxAHEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigAooooAKKKKACiiigAoNFYnibWG0a2sJF6XF/BbsfRWbB/SgDbooooAKKKKACiiigApDRmqGs6vbaJpkl/d7/KjIGEXLEk4AA7nJoA0KKgtLqG9tUuIHDxOMqw71NQAtFGaTPFIBaSjNGaYC1ynjyxOoWOk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QN8ZvCwXIN2fYQ//AF66f/hDtAyD/Zlvx0+WnjwnoQwf7Mtzj/Yos+4e8cc3xs8NgfJbag59oR/jVeT43aSEzFpWouew8sD+td8vhrRkOV022/74FTLoemL0sLcf9sxRyhqeYt8bWfP2fw3eN6Fv/rZrV8J/EjU/EfiO306bQJbSCUMWmYnC4UkdvbFd+mm2UYwlpCB7IKmSCKNgUjRfooFCQWZJRRRVFCd6KWigBKKWjvQAlFLRQAmKKWigBKKWigBKWiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA//2Q==

as the URL for an image, as that isn't a URL. Both OpenURI and Net::HTTP expect a URL to the image, which they will then request and read the resulting response, returning the data back to your code. You'd need to do a Base64 decode against that data, which will result in a binary string in memory, which you can then write to a file opened in binary mode.