# How to fetch images from web page with relative path

**URL:** <https://www.ruby-forum.com/t/how-to-fetch-images-from-web-page-with-relative-path/147778>\
**Category:** Ruby\
**Created:** [September 5, 2008, 9:44am UTC](https://www.ruby-forum.com/t/how-to-fetch-images-from-web-page-with-relative-path/147778 "2008-09-05T09:44:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yzheng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yzheng](https://www.ruby-forum.com/u/yzheng)\
**Post date:** [September 5, 2008, 9:44am UTC](https://www.ruby-forum.com/t/how-to-fetch-images-from-web-page-with-relative-path/147778/1 "2008-09-05T09:44:27Z")

</div>

Hi, all

here is the code, it works when the image has a full path, but I met  
some pages which only gives relative path for images. How to handle this  
condition?

Thanks a lot!

def self.get\_magick\_img\_from\_url(url)  
begin  
blob =’’  
img\_file = nil  
Timeout::timeout(5) {  
#open web page  
img\_file = open(url)  
}  
if img\_file  
#read page content and store into blob  
img\_file.readlines().each() do |line|  
blob \<\< line  
end  
img\_file.close()  
#fetch images here  
return Magick::Image.from\_blob(blob)  
end  
rescue Timeout::Error  
puts 'Timeout in get\_magick\_img\_from\_url for url - ’ + url  
rescue  
puts 'Error in get\_magick\_img\_from\_url - ’ + $!  
end  
return nil  
end

---

<div class="post-metadata">

**Author:** ![yzheng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yzheng](https://www.ruby-forum.com/u/yzheng)\
**Post date:** [September 5, 2008, 9:47am UTC](https://www.ruby-forum.com/t/how-to-fetch-images-from-web-page-with-relative-path/147778/2 "2008-09-05T09:47:45Z")

</div>

BTW, I use rmagick.

---

<div class="post-metadata">

**Author:** ![yzheng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yzheng](https://www.ruby-forum.com/u/yzheng)\
**Post date:** [September 5, 2008, 10:52am UTC](https://www.ruby-forum.com/t/how-to-fetch-images-from-web-page-with-relative-path/147778/3 "2008-09-05T10:52:18Z")

</div>

Yi Zheng wrote:

> BTW, I use rmagick.

What do you use to get the links ?

---

<div class="post-metadata">

**Author:** ![yzheng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yzheng](https://www.ruby-forum.com/u/yzheng)\
**Post date:** [September 5, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/how-to-fetch-images-from-web-page-with-relative-path/147778/4 "2008-09-05T12:21:34Z")

</div>

Yi Zheng wrote:

> Hi Lex,
> 
> I use open-uri, any tip about this?

Would you consider switching to mechanize ? It can handle relative links  
as well as absolute ones . With mechanize you could get all the pictures  
in a very easy way .

---

<div class="post-metadata">

**Author:** ![yzheng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yzheng](https://www.ruby-forum.com/u/yzheng)\
**Post date:** [September 5, 2008, 10:54am UTC](https://www.ruby-forum.com/t/how-to-fetch-images-from-web-page-with-relative-path/147778/5 "2008-09-05T10:54:34Z")

</div>

Hi Lex,

I use open-uri, any tip about this?

---

<div class="post-metadata">

**Author:** ![yzheng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yzheng](https://www.ruby-forum.com/u/yzheng)\
**Post date:** [April 20, 2009, 11:49am UTC](https://www.ruby-forum.com/t/how-to-fetch-images-from-web-page-with-relative-path/147778/6 "2009-04-20T11:49:52Z")

</div>

Hi All,

Get all images from given URL

Desktop Application from jazzez

Kindly go through below link and download the EXE file.

> **[jazzez-image-v2.exe | Powered by Box](https://app.box.com/s/m5vxqpr3r1)**
>
> Get all images from the given URL

Install in Windows machine and enjoy with impressed images

Regards,  
P.Raveendran

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

---

<div class="post-metadata">

**Author:** ![yzheng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yzheng](https://www.ruby-forum.com/u/yzheng)\
**Post date:** [September 7, 2008, 2:18pm UTC](https://www.ruby-forum.com/t/how-to-fetch-images-from-web-page-with-relative-path/147778/7 "2008-09-07T14:18:14Z")

</div>

Lex W. wrote:

> Yi Zheng wrote:
> 
> > Hi Lex,
> > 
> > I use open-uri, any tip about this?
> 
> Would you consider switching to mechanize ? It can handle relative links  
> as well as absolute ones . With mechanize you could get all the pictures  
> in a very easy way .

Thanks Lex, I will try.
