[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

getting absolute path ?

Stef Mientki

1/9/2008 9:23:00 PM

hello,

I'm trying to convert the links in html pages to absolute links,
these pages can either be webpages or files on local harddisk (winXP).
Now I've struggling for a while, and this code works a lilttle:

i = line.find ( 'href=' )
if i < 0 :
i = line.find ( ' src=' )
if i >= 0 :
ii = line.find ( '"', i+6 )
file = line [ i+6 : ii ]
#print urlparse.urljoin ( p, file )
if file.find ( 'http:' ) < 0 :
abspath = os.path.normpath ( os.path.join ( p, file ) )
line = line.replace ( file, abspath )
print line

but it only covers files on local disk and just 1 link per line,
so I guess it's a lot of trouble to catch all cases.
Isn't there a convenient function for (OS independent preferable) ?
Googled for it, but can't find it.

thanks,
Stef Mientki
2 Answers

Mike Driscoll

1/9/2008 9:57:00 PM

0

On Jan 9, 3:22 pm, Stef Mientki <stef.mien...@gmail.com> wrote:
> hello,
>
> I'm trying to convert the links in html pages to absolute links,
> these pages can either be webpages or files on local harddisk (winXP).
> Now I've struggling for a while, and this code works a lilttle:
>
> i = line.find ( 'href=' )
> if i < 0 :
> i = line.find ( ' src=' )
> if i >= 0 :
> ii = line.find ( '"', i+6 )
> file = line [ i+6 : ii ]
> #print urlparse.urljoin ( p, file )
> if file.find ( 'http:' ) < 0 :
> abspath = os.path.normpath ( os.path.join ( p, file ) )
> line = line.replace ( file, abspath )
> print line
>
> but it only covers files on local disk and just 1 link per line,
> so I guess it's a lot of trouble to catch all cases.
> Isn't there a convenient function for (OS independent preferable) ?
> Googled for it, but can't find it.
>
> thanks,
> Stef Mientki

I googled a bit too. The Perl forums talk about using a regular
expression. You can probably take that and translate it into the
Python equivalent:

http://forums.devshed.com/perl-programming-6/how-to-parse-relatives-links-to-absolute-links...

I also found this, which appears to be an old c.l.py thread:

http://www.dbforums.com/archive/index.php/t-3...

You might have more luck if you google for "relative to absolute
links". I would also take a look at how django or cherrypy creates
their URLs.

Mike

Stef Mientki

1/10/2008 11:31:00 PM

0

thanks Mike,

with your links I managed to write some code that seems to work well.
Still I stay surprised that these kind of functions are not available ;-)
cheers,
Stef

kyosohma@gmail.com wrote:
> On Jan 9, 3:22 pm, Stef Mientki <stef.mien...@gmail.com> wrote:
>
>> hello,
>>
>> I'm trying to convert the links in html pages to absolute links,
>> these pages can either be webpages or files on local harddisk (winXP).
>> Now I've struggling for a while, and this code works a lilttle:
>>
>> i = line.find ( 'href=' )
>> if i < 0 :
>> i = line.find ( ' src=' )
>> if i >= 0 :
>> ii = line.find ( '"', i+6 )
>> file = line [ i+6 : ii ]
>> #print urlparse.urljoin ( p, file )
>> if file.find ( 'http:' ) < 0 :
>> abspath = os.path.normpath ( os.path.join ( p, file ) )
>> line = line.replace ( file, abspath )
>> print line
>>
>> but it only covers files on local disk and just 1 link per line,
>> so I guess it's a lot of trouble to catch all cases.
>> Isn't there a convenient function for (OS independent preferable) ?
>> Googled for it, but can't find it.
>>
>> thanks,
>> Stef Mientki
>>
>
> I googled a bit too. The Perl forums talk about using a regular
> expression. You can probably take that and translate it into the
> Python equivalent:
>
> http://forums.devshed.com/perl-programming-6/how-to-parse-relatives-links-to-absolute-links...
>
> I also found this, which appears to be an old c.l.py thread:
>
> http://www.dbforums.com/archive/index.php/t-3...
>
> You might have more luck if you google for "relative to absolute
> links". I would also take a look at how django or cherrypy creates
> their URLs.
>
> Mike
>