getting full URL from relative links

S

slugger3113

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
 
S

Steve C

slugger3113 said:
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.
 
C

C.DeRykus

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/')
 
S

slugger3113

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top