杰瑞科技汇

Python中如何使用querystring?

Of course! In Python, the standard library for working with query strings is urllib.parse. A "query string" is the part of a URL that comes after the character, used to send data to a web server.

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

For example, in the URL https://www.example.com/search?q=python+tutorial&page=2, the query string is q=python+tutorial&page=2.

Here’s a complete guide covering the most common tasks.

The Core Module: urllib.parse

You'll need to import this module to get started.

import urllib.parse

Parsing a Query String (String to Dictionary)

This is the most common task. You have a query string and you want to convert it into a Python dictionary for easy access to the values.

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

The function for this is urllib.parse.parse_qs().

Key Feature: Handling Multiple Values

A crucial thing to understand is that parse_qs() returns a dictionary where each value is a list. This is because a query parameter can appear multiple times (e.g., color=red&color=blue).

Example: parse_qs()

import urllib.parse
query_string = "q=python+tutorial&page=2&filter=beginner&filter=advanced"
# Parse the query string into a dictionary
parsed_query = urllib.parse.parse_qs(query_string)
print(f"Original Query String: {query_string}\n")
print(f"Parsed Dictionary: {parsed_query}\n")
# Accessing the values
# Remember, values are lists!
search_term = parsed_query['q'][0]
page_number = parsed_query['page'][0]
filters = parsed_query['filter'] # This is already a list
print(f"Search Term: {search_term}")
print(f"Page Number: {page_number}")
print(f"Filters: {filters}")

Output:

Original Query String: q=python+tutorial&page=2&filter=beginner&filter=advanced
Parsed Dictionary: {'q': ['python tutorial'], 'page': ['2'], 'filter': ['beginner', 'advanced']}
Search Term: python tutorial
Page Number: 2
Filters: ['beginner', 'advanced']

What if you only want the first value and are sure there are no duplicates?

You can use a dictionary comprehension to clean this up.

Python中如何使用querystring?-图3
(图片来源网络,侵删)
# Create a "flat" dictionary where only the first value is kept
flat_query = {key: value[0] for key, value in parsed_query.items()}
print(f"Flat Dictionary: {flat_query}")

Output:

Flat Dictionary: {'q': 'python tutorial', 'page': '2', 'filter': 'beginner'}

Building a Query String (Dictionary to String)

This is the reverse operation. You have a Python dictionary and you want to turn it into a properly formatted query string.

The function for this is urllib.parse.urlencode().

Example: urlencode()

import urllib.parse
# Your data as a Python dictionary
data = {
    'user_id': '123',
    'action': 'delete',
    'confirm': 'true'
}
# Encode the dictionary into a query string
query_string = urllib.parse.urlencode(data)
print(f"Original Dictionary: {data}\n")
print(f"Generated Query String: {query_string}")

Output:

Original Dictionary: {'user_id': '123', 'action': 'delete', 'confirm': 'true'}
Generated Query String: user_id=123&action=delete&confirm=true

Handling Lists of Values

urlencode() is smart. If a value in your dictionary is a list, it will automatically create multiple key-value pairs in the query string.

import urllib.parse
# A dictionary with a list value
data_with_list = {
    'product_id': 'xyz-987',
    'color': ['red', 'blue', 'green']
}
# Encode the dictionary
query_string_with_list = urllib.parse.urlencode(data_with_list, doseq=True)
print(f"Original Dictionary: {data_with_list}\n")
print(f"Generated Query String: {query_string_with_list}")

Note: The doseq=True argument is important. It tells urlencode to handle sequences (like lists) by expanding them. Without it, the list ['red', 'blue'] would be URL-encoded as the string ["red", "blue"], which is not what you want.

Output:

Original Dictionary: {'product_id': 'xyz-987', 'color': ['red', 'blue', 'green']}
Generated Query String: product_id=xyz-987&color=red&color=blue&color=green

Parsing a Full URL

Often, you have a complete URL and you need to extract just the query string part before you can parse it.

The function urllib.parse.urlparse() is perfect for this. It splits a URL into its components (scheme, netloc, path, params, query, fragment).

Example: urlparse()

import urllib.parse
full_url = "https://www.example.com/path/to/resource?search=python&limit=10#results"
# Parse the full URL
parsed_url = urllib.parse.urlparse(full_url)
print(f"Original URL: {full_url}\n")
print(f"Parsed URL Object: {parsed_url}\n")
# The query string is in the 'query' attribute
query_string = parsed_url.query
print(f"Extracted Query String: '{query_string}'")
# Now you can parse this query string as shown in section 1
query_params = urllib.parse.parse_qs(query_string)
print(f"Parsed Query Parameters: {query_params}")

Output:

Original URL: https://www.example.com/path/to/resource?search=python&limit=10#results
Parsed URL Object: ParseResult(scheme='https', netloc='www.example.com', path='/path/to/resource', params='', query='search=python&limit=10', fragment='results')
Extracted Query String: 'search=python&limit=10'
Parsed Query Parameters: {'search': ['python'], 'limit': ['10']}

Complete Workflow Example

Here is a realistic example combining these concepts.

import urllib.parse
# 1. You start with a full URL
original_url = "https://api.weather.com/v1/forecast?city=New York&units=metric&days=5"
# 2. Parse the URL to get the query string
parsed_url = urllib.parse.urlparse(original_url)
query_string_from_url = parsed_url.query
print(f"1. Extracted query string from URL: '{query_string_from_url}'")
# 3. Parse the query string into a dictionary
params_dict = urllib.parse.parse_qs(query_string_from_url)
print(f"2. Converted to dictionary: {params_dict}")
# 4. Modify the dictionary (e.g., change units)
params_dict['units'] = ['fahrenheit'] # Change metric to fahrenheit
print(f"3. Modified dictionary: {params_dict}")
# 5. Build a new query string from the modified dictionary
new_query_string = urllib.parse.urlencode(params_dict, doseq=True)
print(f"4. Built new query string: '{new_query_string}'")
# 6. Reconstruct the full URL with the new query string
# We use the original parsed URL components and just replace the query part
new_url = parsed_url._replace(query=new_query_string).geturl()
print(f"\n5. Final new URL: {new_url}")

Output:

Extracted query string from URL: 'city=New York&units=metric&days=5'
2. Converted to dictionary: {'city': ['New York'], 'units': ['metric'], 'days': ['5']}
3. Modified dictionary: {'city': ['New York'], 'units': ['fahrenheit'], 'days': ['5']}
4. Built new query string: 'city=New+York&units=fahrenheit&days=5'
5. Final new URL: https://api.weather.com/v1/forecast?city=New+York&units=fahrenheit&days=5

Summary of Key Functions

Function Purpose Example
urllib.parse.parse_qs(qs) Parses a query string (qs) into a dictionary where values are lists. parse_qs("a=1&b=2") -> {'a': ['1'], 'b': ['2']}
urllib.parse.urlencode(dict) Encodes a dictionary into a query string. Handles lists correctly with doseq=True. urlencode({'a': '1'}) -> 'a=1'
urllib.parse.urlparse(url) Splits a full URL into 6 components (scheme, netloc, path, params, query, fragment). urlparse("...?a=1") -> query='a=1'
分享:
扫描分享到社交APP
上一篇
下一篇