geds133
geds133

Reputation: 1485

efficiently join url in python without string concatenation

I am trying to join a url but at the moment have a very manual way of doing it and wondered if there is a more efficient way.

I have the homepage with I have parsed using urlparse. I also have the end link which I am trying to join with both the scheme and the domain name. The code is as such:

url = 'https://www.windex.com/en-us'
link = '/en-us/products/outdoor-sprayer'

parsed_url = urlparse(url)
full_url = parsed_url.scheme + '://' + parsed_url.netloc + link

This gives https://www.windex.com/en-us/products/outdoor-sprayer which is correct and directs me to the right page. Is there a better way to do this other than string concatenation? I tried urljoin as follows but got the wrong result:

urljoin(parsed_url.scheme, parsed_url.netloc, link)
Output: 'www.windex.com'

Many Thanks

Upvotes: 0

Views: 1266

Answers (1)

furas
furas

Reputation: 142641

If you mean urllib.parse.urljoin then you use it in wrong way because it needs only base and url

import urllib

url = 'https://www.windex.com/en-us/'
link = '/en-us/products/outdoor-sprayer'

full_url = urllib.parse.urljoin(url, link)

Upvotes: 2

Related Questions