I am trying to form URLs from different pieces, and having trouble understanding the behavior of this method. For example:
Python 3.x
from urllib.parse import urljoin
>>> urljoin('some', 'thing')
'thing'
>>> urljoin('http://some', 'thing')
'http://some/thing'
>>> urljoin('http://some/more', 'thing')
'http://some/thing'
>>> urljoin('http://some/more/', 'thing') # just a tad / after 'more'
'http://some/more/thing'
urljoin('http://some/more/', '/thing')
'http://some/thing'
Can you explain the exact behavior of this method?
The best way (for me) to think of this is the first argument,
baseis like the page you are on in your browser. The second argumenturlis the href of an anchor on that page. The result is the final url to which you will be directed should you click.This one makes sense given my description. Though one would hope base includes a scheme and domain.
If you are on a vhost some, and there is an anchor like
<a href='thing'>Foo</a>then the link will take you tohttp://some/thingWe are on
some/morehere, so a relative link ofthingwill take us to/some/thingHere, we aren’t on
some/more, we are onsome/more/which is different. Now, our relative link will take us tosome/more/thingAnd lastly. If on
some/more/and the href is to/thing, you will be linked tosome/thing.