Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > getting full URL from relative links

Reply
Thread Tools

getting full URL from relative links

 
 
slugger3113
Guest
Posts: n/a
 
      04-19-2010
Hi, I'm trying to get full/absolute URLs from relative links in HTML
documents. I've been trying to fudge this using File::Basename,
WWW::Mechanize, etc. but was wondering if there's a more ready-made
way to do this.

For example, if my main doc is:

http://www.abc.com/x/y/z/mydoc.html

and it contains a relative link to:

.../../otherdir/yourdoc.html

how do I get the absolute URL to "yourdoc.html"? Using the above
modules I've been able to get:

http://www.abc.com/x/y/z/../../otherdir/yourdoc.html

when what I want is:

http://www.abc.com/x/otherdir/yourdoc.html

Of course I could try and parse all of the possible variations for
relative paths, but it's making my head hurt and I was wondering if
there's a module that could help with this. Any thoughts would be
appreciated.

thanks
Scott
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-19-2010
slugger3113 <(E-Mail Removed)> wrote:
>http://www.abc.com/x/y/z/../../otherdir/yourdoc.html
>
>when what I want is:
>
>http://www.abc.com/x/otherdir/yourdoc.html


For file names there is a module that will compute the canonical path,
but I can't remember the name right now. And I don't know if it will
work with URLs, either.

jue
 
Reply With Quote
 
 
 
 
Steve C
Guest
Posts: n/a
 
      04-19-2010
slugger3113 wrote:
> Hi, I'm trying to get full/absolute URLs from relative links in HTML
> documents. I've been trying to fudge this using File::Basename,
> WWW::Mechanize, etc. but was wondering if there's a more ready-made
> way to do this.
>
> For example, if my main doc is:
>
> http://www.abc.com/x/y/z/mydoc.html
>
> and it contains a relative link to:
>
> ../../otherdir/yourdoc.html
>
> how do I get the absolute URL to "yourdoc.html"? Using the above
> modules I've been able to get:
>
> http://www.abc.com/x/y/z/../../otherdir/yourdoc.html
>
> when what I want is:
>
> http://www.abc.com/x/otherdir/yourdoc.html
>
> Of course I could try and parse all of the possible variations for
> relative paths, but it's making my head hurt and I was wondering if
> there's a module that could help with this. Any thoughts would be
> appreciated.


You also need to know if there is a base tag in the head section
since that changes the meaning of a relative link.
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      04-19-2010
On Apr 19, 8:51*am, slugger3113 <(E-Mail Removed)> wrote:
> Hi, I'm trying to get full/absolute URLs from relative links in HTML
> documents. I've been trying to fudge this using File::Basename,
> WWW::Mechanize, etc. but was wondering if there's a more ready-made
> way to do this.
>
> For example, if my main doc is:
>
> http://www.abc.com/x/y/z/mydoc.html
>
> and it contains a relative link to:
>
> ../../otherdir/yourdoc.html
>
> how do I get the absolute URL to "yourdoc.html"? Using the above
> modules I've been able to get:
>
> http://www.abc.com/x/y/z/../../otherdir/yourdoc.html
>
> when what I want is:
>
> http://www.abc.com/x/otherdir/yourdoc.html
>
> Of course I could try and parse all of the possible variations for
> relative paths, but it's making my head hurt and I was wondering if
> there's a module that could help with this. Any thoughts would be
> appreciated.
>



See: perldoc URI

eg, print URI->new_abs('../../otherdir/yourdoc.html' ,
'http://www.abc.com/x/y/z/')

--
Charles DeRykus

 
Reply With Quote
 
slugger3113
Guest
Posts: n/a
 
      04-19-2010
On Apr 19, 11:07*am, Jürgen Exner <(E-Mail Removed)> wrote:
> For file names there is a module that will compute the canonical path,
> but I can't remember the name right now. And I don't know if it will
> work with URLs, either.
>
> jue


Hm it looks like File::Spec will do what I want:

my($dpath) = "/one/two/../three/four";

my $cpath = File::Spec->canonpath( $dpath );

print $cpath,$/;

result: /one/three/four

thanks for the tip on "canonical" (whatever that means)!

Scott
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Update to FAQ - Relative URLs for In-page Links and Links to Notes Garrett Smith Javascript 14 05-26-2009 04:50 PM
Resource/File URL Problems, relative links, servlet context and LB Java 1 06-10-2008 08:24 PM
Get full url from relative url David Thielen ASP .Net Web Controls 2 05-26-2006 05:29 PM
Response.Redirect() converts relative URL to absolute URL =?Utf-8?B?UGF1bCBCb25mYW50aQ==?= ASP .Net 2 04-25-2006 01:07 AM
Relative URL's to absolute URL's function ? wl ASP .Net 1 07-14-2004 10:28 AM



Advertisments