When working with web applications, APIs, or scraping websites in Python, you'll inevitably need to handle URLs. Here are some best practices for writing, encoding, parsing, and modifying URLs in Python.
Writing URLs
You can specify URLs in Python using normal strings like:
url = "https://www.example.com/path?key=value"
Prepend
root_url = "https://www.example.com"
path = "/products"
url = root_url + path
Encoding URLs
When including URLs in queries or path segments, ensure special characters are URL encoded using the
from urllib.parse import urlencode
params = {"q": "green eggs & ham"}
print(urlencode(params)) # q=green+eggs+%26+ham
This converts characters like
Parsing URL Components
To manipulate a URL's protocol, hostname, path etc individually, use
from urllib.parse import urlparse
url = "http://user:[email protected]/path?query=true"
parsed = urlparse(url)
print(parsed.scheme) # 'http'
print(parsed.hostname) # 'example.com'
You can modify components of the
Practical Usage
When making HTTP requests with the
The key is using the right tool for the job - Python makes handling URLs easy, as long as you leverage the built-in libraries.