from urllib.parse import urlparse, parse_qs
def aggregate_query_strings(url):
"""
Aggregates values of query strings from a URL with basic input validation.
Args:
url (str): The URL to parse.
Returns:
dict: A dictionary where keys are query string parameters and values are lists of their corresponding values.
Returns an empty dictionary if the URL is invalid or has no query parameters.
"""
try:
parsed_url = urlparse(url)
query_params = parse_qs(parsed_url.query)
# Basic validation: Check if query_params is not None
if query_params is None:
return {}
# Iterate through the parsed query parameters and aggregate values
aggregated_params = {}
for key, values in query_params.items():
if not isinstance(key, str):
print(f"Warning: Invalid key type found: {key}. Skipping.")
continue # Skip if key is not a string
if not values: # Skip empty values
continue
if isinstance(values, list):
aggregated_params[key] = values
else:
print(f"Warning: Unexpected value type for key '{key}'. Skipping.")
continue
return aggregated_params
except Exception as e:
print(f"Error parsing URL: {e}")
return {}
if __name__ == '__main__':
# Example usage
url1 = "https://example.com?param1=value1¶m2=value2¶m1=value3"
url2 = "https://example.com?param1=value1¶m2"
url3 = "https://example.com"
url4 = "invalid_url"
url5 = "https://example.com?param1=value1¶m2=value2¶m3=value1"
url6 = "https://example.com?param1=value1¶m2=value2¶m3=value1¶m4=value1"
print(f"URL: {url1}, Aggregated: {aggregate_query_strings(url1)}")
print(f"URL: {url2}, Aggregated: {aggregate_query_strings(url2)}")
print(f"URL: {url3}, Aggregated: {aggregate_query_strings(url3)}")
print(f"URL: {url4}, Aggregated: {aggregate_query_strings(url4)}")
print(f"URL: {url5}, Aggregated: {aggregate_query_strings(url5)}")
print(f"URL: {url6}, Aggregated: {aggregate_query_strings(url6)}")
Add your comment