When building URLs in Python, you may need to include special characters or spaces that can cause issues if left unencoded. Thankfully, Python's urllib library provides a simple way to encode these characters.
The
import urllib.parse
params = {'q': 'python urllib', 'type': 'articles', 'sort': 'relevance'}
url = 'https://www.example.com/search?' + urllib.parse.urlencode(params)
print(url)
This would print:
https://www.example.com/search?q=python+urllib&type=articles&sort=relevance
The key things urlencode does:
So behind the scenes, it is converting the space in "python urllib" to a
Some common use cases for urlencode:
A few things to watch out for:
To decode an encoded URL string back to a dictionary, use
import urllib.parse
url = "https://www.example.com/search?q=python+urllib&type=articles"
print(urllib.parse.parse_qs(url))
This covers the basics of encoding parameters into URLs with Python. The