Absolute TO Relative URLs
Ben Hutchings
ben.hutchings at roundpoint.com
Tue May 8 23:29:13 EDT 2001
"Satheesh Babu" <vsbabu at erols.com> writes:
> Hi,
>
> I got the first part (SGML Parser and getting A, IMG etc) fairly done. The
> regex part (sigh, I always get stuck with regex) is driving me insane...
You don't need to write them yourself - just use urlparse:
def get_relative_url(url, base_url):
from urlparse import urlparse, urljoin, urlunparse
base_comps = urlparse(base_url)
url_comps = list(urlparse(urljoin(base_url, url)))
different = 0
for i in range(len(url_comps)):
if different or (url_comps[i] and url_comps[i] != base_comps[i]):
different = 1
else:
url_comps[i] = ''
return urlunparse(tuple(url_comps))
Making the actual path part (component number 2) relative is left as
an exercise for the reader.
--
Any opinions expressed are my own and not necessarily those of Roundpoint.
More information about the Python-list
mailing list