杰瑞科技汇

Python querystring如何使用?

Of course! In Python, "querystring" most commonly refers to the part of a URL that comes after the character. It contains key-value pairs used to send data to a web server.

Python querystring如何使用?-图1
(图片来源网络,侵删)

Here's a comprehensive guide on how to work with querystrings in Python, covering the two main scenarios:

  1. Parsing a querystring from a URL (getting the data).
  2. Building a querystring (creating the data).

Parsing a Querystring (Decoding)

This is the process of taking a URL with a querystring and converting it into a Python dictionary.

The Modern & Recommended Way: urllib.parse

Since Python 3, the standard library module for this is urllib.parse. It's powerful, handles edge cases, and is the recommended approach.

Key Functions:

Python querystring如何使用?-图2
(图片来源网络,侵删)
  • urlparse(): Parses a URL into its components (scheme, netloc, path, params, query, fragment).
  • parse_qs(): Parses a querystring into a dictionary where each value is a list. This is important because a key can appear multiple times (e.g., ?name=John&name=Doe).
  • parse_qsl(): Parses a querystring into a list of (key, value) tuples.
  • unquote(): Decodes percent-encoded characters (e.g., %20 becomes a space).

Example 1: Basic Parsing

Let's say you have this URL: https://www.example.com/search?user_id=123&q=python%20tutorial&page=2

from urllib.parse import urlparse, parse_qs
url = "https://www.example.com/search?user_id=123&q=python%20tutorial&page=2"
# 1. Parse the URL to isolate the querystring
parsed_url = urlparse(url)
query_string = parsed_url.query
print(f"Isolated Querystring: '{query_string}'")
# Output: Isolated Querystring: 'user_id=123&q=python%20tutorial&page=2'
# 2. Parse the querystring into a dictionary
# parse_qs returns a dictionary where values are lists
query_dict = parse_qs(query_string)
print("\nParsed Dictionary (using parse_qs):")
print(query_dict)
# Output:
# Parsed Dictionary (using parse_qs):
# {
#     'user_id': ['123'],
#     'q': ['python tutorial'],
#     'page': ['2']
# }
# To get a single value, you must access the first element of the list
user_id = query_dict['user_id'][0]
search_query = query_dict['q'][0]
print(f"\nUser ID: {user_id}")
print(f"Search Query: {search_query}")
# Output:
# User ID: 123
# Search Query: python tutorial

Example 2: Handling Multiple Values for a Key

If a key appears multiple times, parse_qs correctly captures all values.

from urllib.parse import parse_qs
url = "https://api.example.com/data?filter=active&filter=pending&sort=asc"
query_dict = parse_qs(url.split('?')[1]) # A quick way to get the query part
print(query_dict)
# Output:
# {
#     'filter': ['active', 'pending'],
#     'sort': ['asc']
# }
# To get all filters:
filters = query_dict['filter']
print(f"All filters: {filters}")
# Output: All filters: ['active', 'pending']

Example 3: Using parse_qsl for Tuples

If you prefer working with a list of tuples, parse_qsl is perfect.

from urllib.parse import parse_qsl
url = "https://www.example.com/search?user_id=123&q=python&page=2"
query_tuples = parse_qsl(url.split('?')[1])
print(query_tuples)
# Output:
# [
#     ('user_id', '123'),
#     ('q', 'python'),
#     ('page', '2')
# ]

Building a Querystring (Encoding)

This is the process of taking a Python dictionary (or list of tuples) and converting it into a properly formatted querystring for use in a URL.

The Modern & Recommended Way: urllib.parse

You'll use the urlencode() function. It automatically handles URL-encoding special characters (like spaces becoming %20) and joining key-value pairs with &.

Example 1: Building from a Dictionary

from urllib.parse import urlencode
# Data you want to send as a querystring
params = {
    'user_id': '123',
    'q': 'python tutorial',
    'page': '2'
}
# Build the querystring
querystring = urlencode(params)
print(querystring)
# Output: user_id=123&q=python+tutorial&page=2
# Note: Spaces are converted to '+' by default, which is valid.
# You can also use `quote_via` to use %20 encoding instead
querystring_with_percent = urlencode(params, quote_via=urllib.parse.quote)
print(querystring_with_percent)
# Output: user_id=123&q=python%20tutorial&page=2

Example 2: Using the Querystring in a Full URL

Now you can easily append it to a base URL.

from urllib.parse import urlencode, urljoin
base_url = "https://www.example.com/search"
params = {'q': 'advanced python', 'sort': 'date'}
# 1. Build the querystring
querystring = urlencode(params)
# 2. Combine with the base URL
# Using f-string is simple and clear
full_url = f"{base_url}?{querystring}"
print(full_url)
# Output: https://www.example.com/search?q=advanced+python&sort=date

The "Old Way" (Python 2)

For historical purposes, you might see urlparse and urlencode from the cgi module. Do not use this in new Python 3 code. It was deprecated and removed in Python 3.11.

Python 2 (Old & Incorrect for Python 3):

import cgi  # DO NOT USE IN PYTHON 3
params = {'q': 'python', 'page': '2'}
querystring = cgi.urlencode(params) # Old way

Complete Example: Putting It All Together

Here is a practical example of a script that builds a URL, parses it, and then builds it again.

from urllib.parse import urlparse, parse_qs, urlencode
# --- Part 1: Building a URL ---
print("--- Building a URL ---")
api_endpoint = "https://api.weatherapi.com/v1/current.json"
my_params = {
    'key': 'YOUR_API_KEY',
    'q': 'London',
    'aqi': 'yes'
}
# Create the querystring
querystring = urlencode(my_params)
# Combine with the endpoint
full_url = f"{api_endpoint}?{querystring}"
print(f"The complete URL is:\n{full_url}\n")
# --- Part 2: Parsing the URL (as if we received it) ---
print("--- Parsing the URL ---")
# Let's pretend 'full_url' came from somewhere else
# First, we parse the URL to get its components
parsed_url = urlparse(full_url)
# The querystring is in the 'query' attribute
retrieved_querystring = parsed_url.query
print(f"The retrieved querystring is: '{retrieved_querystring}'")
# Now, parse the querystring into a dictionary
params_from_url = parse_qs(retrieved_querystring)
print("\nParameters parsed from the URL:")
print(params_from_url)
# Access a specific value
location = params_from_url['q'][0]
print(f"\nThe location requested was: {location}")

Summary Table

Task Recommended Python 3 Function Module Notes
Parse Querystring parse_qs() urllib.parse Returns a dict with list values. Best for handling multiple keys.
Parse Querystring parse_qsl() urllib.parse Returns a list of (key, value) tuples.
Isolate Querystring urlparse(url).query urllib.parse First step before parsing.
Build Querystring urlencode() urllib.parse Takes a dict or list of tuples. Automatically encodes special chars.
Decode a value unquote('%20') -> urllib.parse Used internally by parse_qs, but good to know.
分享:
扫描分享到社交APP
上一篇
下一篇