What is Urlencode in Python?

Feb 20, 2024 ยท 2 min read

When building URLs in Python, you may occasionally need to encode special characters to ensure they transmit properly. For example, spaces need to be converted to %20 and ampersands to %26. Thankfully, Python's urllib module provides simple ways to handle URL encoding.

Why Encode URLs?

URLs only allow certain alphanumeric characters like letters, numbers, and some symbols such as -, _, ., and ~. Everything else, including spaces, foreign letters, and special symbols, need to be encoded.

For example, an URL with spaces like www.example.com/path with spaces would cause issues. Encoding it gives us:

www.example.com/path%20with%20spaces

This ensures special characters transmit safely through networks and servers can properly interpret them.

Python's urllib for URL Encoding

Python's urllib module provides the simple urlencode() function for encoding parameters and values in URLs:

from urllib.parse import urlencode

params = {"name": "John Wick", "category": "Action & Adventure"}
print(urlencode(params))

# name=John+Wick&category=Action+%26+Adventure

We can also manually encode pieces of URLs as needed:

from urllib.parse import quote_plus

url = "http://localhost:8000/movies/" + quote_plus("Science Fiction") 
# http://localhost:8000/movies/Science%20Fiction

The quote_plus() method handles spaces, special characters, and even non-ASCII letters.

When to Encode URLs

  • When building URLs with user-supplied input that may contain spaces or special symbols
  • When transmitting non-English characters
  • When connecting URLs paths that may have spaces or special characters
  • Getting in the habit of encoding URLs ensures your applications handle edge cases safely!

    I tried to provide some practical examples on why and how to encode URLs in Python, along with tips on specific methods that can help.

    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: