Handling URL Encoding in Python Requests

Feb 3, 2024 ยท 2 min read

When making HTTP requests in Python using the Requests module, you may sometimes encounter issues with special characters in URLs not being handled properly. One common example is the ampersand (&) character causing errors. This occurs due to the ampersand having a special meaning in URLs for separating URL parameters.

Here is an example that fails:

import requests

url = 'https://www.example.com/path?key=value&key2=value2'
response = requests.get(url) # Fails with invalid URL error

The reason this fails is that Requests does not automatically URL encode the parameters, so the & is treated as part of the URL path rather than a separator.

The Solution - Use quote_plus

The solution is to manually URL encode the parameters before making the request. The quote_plus function from Python's urllib can be used:

from urllib.parse import quote_plus

url = 'https://www.example.com/path?'+ quote_plus('key=value&key2=value2') 
response = requests.get(url) # Succeeds

The quote_plus function will encode all special characters properly so the URL can be parsed correctly.

An alternative is to use parameters in the params argument rather than directly in the URL:

params = {'key': 'value', 'key2': 'value2'}
response = requests.get('https://www.example.com/path', params=params) 

Other Special Characters

The ampersand is the most common issue, but quote_plus can also handle encoding other special characters like spaces, slashes etc. So it's good practice to always use quote_plus or params argument when putting non-standard values in URLs with Requests.

By handling the URL encoding, you can make Requests work smoothly even with special characters in parameters.

Browse by tags:

Browse by language:

Tired of getting blocked while scraping the web?

ProxiesAPI handles headless browsers and rotates proxies for you.
Get access to 1,000 free API credits, no credit card required!