views:

83

answers:

1

Suppose I have

window_location = 'http://stackoverflow.com/questions/ask'
href = '/users/48465/jader-dias'

I want to obtain

link = 'http://stackoverflow.com/users/48465/jader-dias'

How do I do it in Python?

It have to work just as it works in the browser

+6  A: 
>>> import urlparse
>>> urlparse.urljoin('http://stackoverflow.com/questions/ask',
...                  '/users/48465/jader-dias')
'http://stackoverflow.com/users/48465/jader-dias'

From the doc page of urlparse.urljoin:

urlparse.urljoin(base, url[, allow_fragments])

Construct a full (“absolute”) URL by combining a “base URL” (base) with another URL (url). Informally, this uses components of the base URL, in particular the addressing scheme, the network location and (part of) the path, to provide missing components in the relative URL.

If url is an absolute URL (that is, starting with // or scheme://), the url‘s host name and/or scheme will be present in the result.

Ayman Hourieh
it worked! thanks
Jader Dias