杰瑞科技汇

python query string

Of course! In Python, a "query string" refers to the part of a URL that comes after the character. It's used to send data to a web server in the form of key-value pairs.

python query string-图1
(图片来源网络,侵删)

For example, in the URL https://www.example.com/search?query=python&page=1:

  • The query string is query=python&page=1.
  • The key-value pairs are query=python and page=1.

Python's standard library has two main modules for handling query strings:

  1. urllib.parse: The standard, built-in library. It's perfect for most use cases.
  2. requests: A very popular third-party library for making HTTP requests. It has excellent, easy-to-use tools for query strings.

Let's break down how to use both.


Using urllib.parse (Standard Library)

This module is part of Python's standard library, so no installation is needed. It's great for parsing and building query strings without making any network requests.

python query string-图2
(图片来源网络,侵删)

A. Parsing a Query String (from a URL to a dictionary)

When you receive a URL, you often want to extract the query parameters into a Python dictionary for easy access.

from urllib.parse import urlparse, parse_qs, parse_qsl
url = "https://www.example.com/search?query=python&page=1&category=books"
# 1. Parse the URL to get the query string part
parsed_url = urlparse(url)
query_string = parsed_url.query
print(f"Raw Query String: {query_string}")  # Output: query=python&page=1&category=books
# 2. Parse the query string into a dictionary
#    parse_qs returns a dictionary where values are lists (to handle multiple values per key)
query_params = parse_qs(query_string)
print("\nUsing parse_qs (values as lists):")
print(query_params)
# Output:
# {
#   'query': ['python'],
#   'page': ['1'],
#   'category': ['books']
# }
# To get a single value for a key (assuming there's only one):
search_query = query_params.get('query', [None])[0]
print(f"\nSearch Query: {search_query}") # Output: Search Query: python
# 3. Parse the query string into a list of (key, value) tuples
#    This is useful if you need to preserve the order or handle multiple values individually
query_tuples = parse_qsl(query_string)
print("\nUsing parse_qsl (list of tuples):")
print(query_tuples)
# Output:
# [('query', 'python'), ('page', '1'), ('category', 'books')]

Key Points:

  • parse_qs is most common because it groups values by key. It returns values as lists because a key can appear multiple times (e.g., ?color=red&color=blue).
  • parse_qsl returns a list of tuples, preserving the order of parameters.

B. Building a Query String (from a dictionary to a URL string)

When you need to send data to a server, you'll start with a dictionary and build the query string.

from urllib.parse import urlencode
params = {
    'query': 'python tutorials',
    'page': 2,
    'sort': 'relevance',
    'language': 'en'
}
# 1. Encode the dictionary into a query string
#    By default, urlencode uses 'application/x-www-form-urlencoded' encoding
query_string = urlencode(params)
print(f"Encoded Query String: {query_string}")
# Output: query=python+tutorials&page=2&sort=relevance&language=en
# 2. Use it in a full URL
base_url = "https://www.example.com/search"
full_url = f"{base_url}?{query_string}"
print(f"\nFull URL: {full_url}")
# Output: Full URL: https://www.example.com/search?query=python+tutorials&page=2&sort=relevance&language=en
# 3. Handling lists of values (e.g., for checkboxes or multiple selections)
params_with_list = {
    'topic': ['programming', 'data-science'],
    'user_id': '123'
}
query_string_with_list = urlencode(params_with_list, doseq=True)
print(f"\nQuery String with List: {query_string_with_list}")
# Output: Query String with List: topic=programming&topic=data-science&user_id=123

Key Points:

python query string-图3
(图片来源网络,侵删)
  • urlencode automatically handles URL-encoding (e.g., spaces become or %20).
  • The doseq=True argument is crucial for correctly handling dictionary values that are lists.

Using the requests Library (Third-Party)

The requests library simplifies making HTTP requests and provides a more convenient way to handle query strings.

First, you need to install it:

pip install requests

A. Sending a Request with Query Parameters

You can pass a dictionary of parameters directly to the params argument of a requests method. requests will automatically URL-encode everything and build the final URL for you.

import requests
url = "https://httpbin.org/get" # A test service that echoes back the request
# Define your parameters in a dictionary
params = {: 'Learning Python',
    'author': 'Jane Doe',
    'published': True
}
# Make the GET request. requests builds the query string automatically.
response = requests.get(url, params=params)
# The final URL that was requested (after encoding)
print(f"Requested URL: {response.url}")
# Output: Requested URL: https://httpbin.org/get?title=Learning+Python&author=Jane+Doe&published=true
# The response from the server (it will echo back the params)
print("\nServer Response (JSON):")
print(response.json())

B. Parsing a Query String from a Response URL

If you have a URL and want to parse its query string, requests doesn't have a special function for this. You should still use the standard urllib.parse module, as shown in the first section.

import requests
from urllib.parse import parse_qs
# Let's use the httpbin.org/get example again
url = "https://httpbin.org/get?name=John%20Doe&age=30"
# Make a request to get the URL (or you can just have a URL string)
response = requests.get(url)
# Use urllib.parse to parse the query string from the response's URL
query_params = parse_qs(response.url.query)
print("Parsed query parameters from response URL:")
print(query_params)
# Output:
# {
#   'name': ['John Doe'],  # %20 is decoded back to a space
#   'age': ['30']
# }

Summary: Which to Use?

Task urllib.parse requests
Parsing a query string (from a URL) Best choice. Use parse_qs or parse_qsl. Not its primary purpose. Use urllib.parse instead.
Building a query string (for a URL) Best choice. Use urlencode. Good. Use the params argument when making a request.
Making an HTTP request with parameters More manual (build URL, then use urllib.request). Excellent choice. Pass a dict to the params argument.

In short:

  • Use urllib.parse for any pure string manipulation of URLs and query strings.
  • Use the requests library when your goal is to make an HTTP request and conveniently pass along query parameters.
分享:
扫描分享到社交APP
上一篇
下一篇