# Hpricot Relative Path

**URL:** <https://www.ruby-forum.com/t/hpricot-relative-path/185069>\
**Category:** Ruby\
**Created:** [March 12, 2010, 10:22pm UTC](https://www.ruby-forum.com/t/hpricot-relative-path/185069 "2010-03-12T22:22:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jcain](https://www.ruby-forum.com/u/jcain)\
**Post date:** [March 12, 2010, 10:22pm UTC](https://www.ruby-forum.com/t/hpricot-relative-path/185069/1 "2010-03-12T22:22:39Z")

</div>

I’m trying to write a script that pulls out an image from a yfrog page

So this is what I have

require ‘rubygems’  
require ‘hpricot’  
require ‘open-uri’

url = ‘[ImageShack - Best place for all of your image hosting and image sharing needs](http://yfrog.com/03gssacj)’  
doc = Hpricot(open(url))

(doc%“#main\_image”).attributes[‘src’] # =\> “/img3/7036/gssac.jpg”

The problem is that the path is relative.  
I’ve done a little googling, queried my ruby and rails ML archives,  
glanced  
at hpricot code, and looked through the method lists for open-uri and  
hpricot.  
So far, I don’t see anything that looks very useful.  
Is there a way to have it give me the absolute path so that I can  
reference  
the picture later?

The only thing I’ve found that works so far involves string  
manipulation,  
which seems like a brittle workaround to replace something that probably  
exists if I could just find it.

url = ‘[ImageShack - Best place for all of your image hosting and image sharing needs](http://yfrog.com/03gssacj)’  
page = open(url)  
base = page.base\_uri.to\_s[/(?:http://)?[^/]\*// ] # =\> "  
[http://img3.yfrog.com/](http://img3.yfrog.com/)"  
relative = (Hpricot(page)%“#main\_image”).attributes[‘src’] # =\>  
“/img3/7036/gssac.jpg”  
absolute = URI.join( base , relative )  
absolute.to\_s # =\> “[http://img3.yfrog.com/img3/7036/gssac.jpg](http://img3.yfrog.com/img3/7036/gssac.jpg)”

Anyone know of a better solution?

---

<div class="post-metadata">

**Author:** ![jcain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jcain](https://www.ruby-forum.com/u/jcain)\
**Post date:** [March 12, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/hpricot-relative-path/185069/2 "2010-03-12T22:54:51Z")

</div>

On Fri, Mar 12, 2010 at 1:22 PM, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> The problem is that the path is relative.  
> I’ve done a little googling, queried my ruby and rails ML archives, glanced  
> at hpricot code, and looked through the method lists for open-uri and  
> hpricot.  
> So far, I don’t see anything that looks very useful.  
> Is there a way to have it give me the absolute path so that I can reference  
> the picture later?

Hpricot is just telling you what’s in the HTML. Munging the  
document’s contents are your responsibility, not the parser’s 🙂

> The only thing I’ve found that works so far involves string manipulation,  
> which seems like a brittle workaround to replace something that probably  
> exists if I could just find it.

Look into the URI library.

require ‘uri’

uri = URI.parse( “[ImageShack - Best place for all of your image hosting and image sharing needs](http://yfrog.com/03gssacj)” )  
uri.path = # your hpricot magic to get the image path goes here

Ben

---

<div class="post-metadata">

**Author:** ![jcain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jcain](https://www.ruby-forum.com/u/jcain)\
**Post date:** [March 13, 2010, 12:23am UTC](https://www.ruby-forum.com/t/hpricot-relative-path/185069/3 "2010-03-13T00:23:49Z")

</div>

On Fri, Mar 12, 2010 at 3:54 PM, Ben B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> uri = URI.parse( “[ImageShack - Best place for all of your image hosting and image sharing needs](http://yfrog.com/03gssacj)” )  
> uri.path = # your hpricot magic to get the image path goes here
> 
> Ben

Thanks, this is what I am using now:

page = open url  
image\_path = URI.parse page.base\_uri.to\_s.sub( %r(/$) , ‘’ )  
image\_path.path = (Hpricot(page)%“#main\_image”).attributes[‘src’]  
image\_path.to\_s

It still seems a little excessive, but it’s a lot better than what I had  
before.
