12

I am having a simple doubt.. I am trying to join three parts of a string using urljoin..

   host = "http://foo.com:port"
   ver = "/v1"
   exten = "/path"

Rather than doing host+ver+exten, I want to use urljoin to generate url But urljoin is giving http://foo.com:poort/v1 ( if i try urljoin(host,ver,exten) )

3
  • The docs seem like that's not how urljoin works. Maybe try urlunsplit Commented Jul 17, 2014 at 22:42
  • @Yep_It's_Me: Not sure, i understand.. can you give an example.. urlunsplit will split the url?? Right? Commented Jul 17, 2014 at 22:48
  • Sorry. Urlunsplit doesn't do quite what I thought it does. It will only join tuple of type `urlparse.SplitResult'. My bad. Commented Jul 17, 2014 at 23:00

4 Answers 4

4

Here's one way to do it on linux(python 2.x):

import urlparse
import os
def url_join(host, version, *additional_path):
    return urlparse.urljoin(host, os.path.join(version, *additional_path))

and then call this function like:

>> url_join("http://example.com:port", "v1", "path1", "path2", "path3")
>> 'http://example.com:port/v1/path1/path2/path3
Sign up to request clarification or add additional context in comments.

2 Comments

os.path.join doesn't use the right separator in Win machines. --> ' 'http://foo.com:port/v1\\path1\\path2\\path3'
Us should use import posixpath. When you import os.path on any nix* you will get posixpath and on any windows you will get ntpath.
3

The way urljoin works is by combining a base URL and another URL. You could try joining the relative paths together with simple string combinations, and then use urljoin to join the host and the combined relative path.

Like:

rel = ver + exten
url = urljoin(host, rel)

Sadly if you want to combine multiple URL paths, you will have to use another library. If you're using a non-Windows machine, you could use the os.path module to join them together just like you would combine a local file path.

2 Comments

Can you suggest any libraries to join multiple URL paths in python?
from urllib.parse import urljoin
1

You can also join your list of parts recursively:

def urljoin(parts):
    if len(parts) > 1:
        parts = [urllib.parse.urljoin(parts[0], parts[1])] + parts[2:]
        return urljoin(parts)

    return parts[0]

Use the function like this:

parts = [
    'https://stackoverflow.com/',
    'questions/24814657/',
    'how-to-do-url-join-in-python-using-multiple-parameters/',
    '41756140#41756140',
]

print(urljoin(parts))
# https://stackoverflow.com/questions/24814657/how-to-do-url-join-in-python-using-multiple-parameters/41756140#41756140

Note that urllib.parse.urljoin() has a bit different behavior than os.path.join() mentioned by @AnukuL.

Comments

0

You could use the str.join function, as suggested in this other answer:

url = urljoin('http://example.com:port', '/'.join(['v1','path']))

If the path segments contains one or more slash /, use str.strip first:

path='/'.join(map(lambda s: s.strip('/'), ["/v1", "/path"]))
url = urljoin('http://example.com:port', path)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.