Working with Query Parameters in Python Requests

Oct 22, 2023 ยท 3 min read

Query parameters allow you to add extra data to your request URLs in a key-value format. They are an essential aspect of making API calls and web scraping in Python. In this comprehensive guide, we'll explore the ins and outs of working with query parameters using the excellent Requests library.

Passing Query Parameters

There are a few different ways to pass query parameters to the requests.get() and other HTTP methods:

Dictionary

The most common approach is to pass a dictionary to the params argument. The keys and values will be encoded into the URL string automatically:

params = {'key1': 'value1', 'key2': 'value2'}
response = requests.get(url, params=params)

List of Tuples

You can also pass a list of tuples. This achieves the same end result:

params = [('key1', 'value1'), ('key2', 'value2')]
response = requests.get(url, params=params)

Appending to URL

For simple cases you can also just append the query string to the URL directly:

url = '<http://example.com?key1=value1&key2=value2>'
response = requests.get(url)

Accessing Query Parameters

Once the request is made, you can access the parameters in a couple ways:

Via Response Object

The response.url contains the full URL with the parameters appended. You can also access the request.params dictionary directly:

response.url
response.request.params

Individual Parameters

To get an individual parameter value, just index into the request.params dict:

key1_value = response.request.params['key1']

This allows full programmatic access to the parameter values.

Encoding Behavior

One nuance of query parameters is that Requests will automatically encode the parameter values when making the request.

For example, spaces will be converted to %20. This allows you to pass values in a readable format without having to manually encode each one.

Query Parameter Limits

There are limits enforced by web servers and browsers on the number and size of query parameters. For example, Nginx's default is a max of 100 parameters and 4KB overall size.

In practice, try to minimize the number of unique parameters and amount of duplicated data. For larger payloads, consider using request bodies instead.

Best Practices

When working with query parameters:

  • Stick to primitive datatypes like strings, numbers, booleans. Avoid complex nested data.
  • If you need larger structured data, use a request body instead.
  • Don't pass sensitive data like API keys as query parameters. Use request headers instead.
  • Watch out for parameter encoding when troubleshooting issues.
  • FAQs

    How do you pass multiple values for the same key?

    Use a list/array or comma-separated string, e.g. params = {'key': ['val1', 'val2']}

    What's the difference between query params and request body?

    Query params added to URL, limited size. Request body is for larger structured data.

    Can query parameters be used in POST requests?

    Yes, query params can be used with any HTTP verb like GET, POST, PUT, etc.

    What are some common use cases for query parameters?

    Pagination, filtering, options, non-sensitive metadata.

    Query parameters provide a way to pass data in a key-value format in your request URLs. Mastering their usage is key for productive web scraping and API integration in Python.

    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: