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:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


Try ProxiesAPI for free

curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
...

X

Don't leave just yet!

Enter your email below to claim your free API key: