473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

getting absolute path ?

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
Jan 9 '08 #1
2 2280
On Jan 9, 3:22 pm, Stef Mientki <stef.mien...@gmail.comwrote:
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-progr...inks-8173.html

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

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

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
Jan 9 '08 #2
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

ky******@gmail.com wrote:
On Jan 9, 3:22 pm, Stef Mientki <stef.mien...@gmail.comwrote:
>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-progr...inks-8173.html

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

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

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
Jan 10 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dchaffin | last post by:
I'm having a problem using file_exists with an absolute path and I can not figure out why. I tried the exact example that is on www.php.net ... <?php $filename = '/path/to/foo.txt'; if...
11
by: KarimL | last post by:
Thanks for your advices... but i need to get the Image height because i dynamically resize the height of my webcontrol based on the image height. More i just have the url (relative parth) to the...
3
by: PJ6 | last post by:
Is it possible to get the absolute path (i.e. C:\inetpub\wwwroot) of the IIS root directory? And yes, I do need to get the actual, absolute path, because the Crystal Reports XI web viewer does -...
2
by: mhadi | last post by:
Hi All, I am developing a solutions for clients to run some webpages. Since there is no webserver the ASP route is a No Go. So I am using Javascript to connect to my database. The Problem is that...
3
by: JeffDotNet | last post by:
I wrote a small data processing application that writes a summary of several hundred files. I use drag and drop on a panel (Panel1) to grab the absolute path to each of these files. Then I begin...
4
windows_mss
by: windows_mss | last post by:
When I Select Source & Destination Dynamically, Path Getting Scatter Across The Map... hi, i can able to get the Correct Route and Path for the corresponding Source and destination, like...
6
by: Jon Slaughter | last post by:
do I have to prefix every absolute path with document root to get it to work? For some reason I thought that prefixing a path with '/' or './' with make it absolute w.r.t to document root but I...
13
by: lawpoop | last post by:
Hello all - I have a two part question. First of all, I have a website under /home/user/www/. The index.php and all the other website pages are under /home/user/www/. For functions that are...
0
by: BruceMcF | last post by:
On Apr 16, 6:03 pm, "Aaron Gray" <ang.use...@gmail.comwrote:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.