Say I have a url that or may not be HTTPS, and who's host name I don't control, but follows a format like: http://example.com/special/content [OR] https://example.com/special/content
Using Python what would be the pythonic way of changing scheme to https and path to /something/else
My current approach is:
from urlparse import urlsplit, urljoin, urlunsplit
currenturl = "http://example.com/some/content"
parts = list(urlsplit(urljoin(currenturl, "/something/else")))
parts[0]="https"
newurl = urlunsplit(parts)
Any suggestions ?
Suggestion (from @ignacio-vazquez-abrams)
from urlparse import urlparse, urljoin, urlunparse
currenturl = "http://example.com/some/content"
parts = list(urlparse(currenturl))
parts[0]="https"
parts[2]="/something/else" # If only path needed changing (or see bellow...)
newurl = urlunparse(parts)
newurl = urljoin(newurl, "/something/else") # If we need to rewrite everything
# after network loc