Before anything else: if you’re not familiar with `urljoin()`, let me give you a quick rundown. This function takes three arguments the base URL (let’s call it “base”), the relative path (let’s call it “path”), and an optional scheme (let’s leave that out for now). It then combines them into a fully qualified URL, like so:
# Import the urlparse module to use the urljoin function
import urlparse
# Define the base URL as a string
base = 'https://www.example.com'
# Define the relative path as a string
path = '/about'
# Use the urljoin function to combine the base URL and relative path into a fully qualified URL
url = urlparse.urljoin(base, path)
# Print the fully qualified URL
print(url) # Output: https://www.example.com/about
Pretty straightforward, right? But what happens when we use it in version 3.5? Well, let’s find out!
First off, if you try to pass a relative URL as the base (i.e., `urljoin(‘http://example.com’, ‘/about’)`), Python will raise an exception with a helpful message: “Cannot join absolute and relative URLs.” This is because in version 3.5, `urljoin()` now checks for this scenario and raises an error instead of silently combining the two URLs (which can lead to unexpected results).
But wait there’s more! If you try to pass a scheme-less base URL (i.e., just “example.com”), Python will also raise an exception with another helpful message: “Cannot join relative and absolute URLs.” This is because in version 3.5, `urljoin()` now checks for this scenario as well and raises an error instead of silently combining the two URLs (which can lead to unexpected results).
So what’s a web developer to do? Well, there are a few options:
1) Always pass a fully qualified base URL with scheme. This ensures that `urljoin()` will always combine the relative path correctly and avoid any potential errors or unexpected behavior.
2) Use another library (like Requests) for more advanced URL manipulation tasks. While `urljoin()` is great for simple cases, it’s not a one-size-fits-all solution for all your web development needs.
3) Embrace the chaos and hope for the best! But seriously if you want to avoid any potential headaches or unexpected results, always pass a fully qualified base URL with scheme when using `urljoin()` in version 3.5 (or later). Trust me, your future self will thank you!