frazman
frazman

Reputation: 33223

how to do url join in python using multiple parameters

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) )

Upvotes: 12

Views: 15263

Answers (4)

lorsanta
lorsanta

Reputation: 118

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)

Upvotes: 0

AnukuL
AnukuL

Reputation: 625

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

Upvotes: 3

Jeyekomon
Jeyekomon

Reputation: 3391

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.

Upvotes: 1

Philip Massey
Philip Massey

Reputation: 1429

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.

Upvotes: 3

Related Questions