Reputation: 39
I am trying to write a function combine three components of a URL: a protocol, location, and resource, into a single URL.
I have the following code, and it works only partially, returning a URL with only the protocol and resource components, but not the location component.
Code:
from urllib.parse import urlparse
import os
def buildURL(protocol, location, resource):
return urllib.parse.urljoin(protocol, os.path.join(location,
resource))
Example: buildURL('http://', 'httpbin.org', '/get')
This returns http:///get
. I a trying to debug this to also allow for the location parameter to be in the URL. It should be returning http://httpbin.org/get
.
How can I build a URL successfully?
Upvotes: 0
Views: 141
Reputation: 137278
You shouldn't be using os.path
here at all. That module is for filesystem paths, e.g. to deal with things like /usr/bin/bash
and C:\Documents and Settings\User\
.
It's not for building URLs. They aren't affected by the user's host OS.
Instead, use urlunparse()
or urlunsplit()
from urllib.parse
:
from urllib.parse import urlunparse
urlunparse(('https', 'httpbin.org', '/get', None, None, None))
# 'https://httpbin.org/get'
Upvotes: 1
Reputation: 5833
It's because you put /get
in the os.path.join
. you should call it like buildURL('http://', 'httpbin.org', 'get')
. os.path.join
will treat /
as an absolute path that will be hooked from the root of the base location, which is the first parameter of the join
function: location
Upvotes: 2