1. from urllib.parse import urlparse, parse_qs
  2. def aggregate_query_strings(url):
  3. """
  4. Aggregates values of query strings from a URL with basic input validation.
  5. Args:
  6. url (str): The URL to parse.
  7. Returns:
  8. dict: A dictionary where keys are query string parameters and values are lists of their corresponding values.
  9. Returns an empty dictionary if the URL is invalid or has no query parameters.
  10. """
  11. try:
  12. parsed_url = urlparse(url)
  13. query_params = parse_qs(parsed_url.query)
  14. # Basic validation: Check if query_params is not None
  15. if query_params is None:
  16. return {}
  17. # Iterate through the parsed query parameters and aggregate values
  18. aggregated_params = {}
  19. for key, values in query_params.items():
  20. if not isinstance(key, str):
  21. print(f"Warning: Invalid key type found: {key}. Skipping.")
  22. continue # Skip if key is not a string
  23. if not values: # Skip empty values
  24. continue
  25. if isinstance(values, list):
  26. aggregated_params[key] = values
  27. else:
  28. print(f"Warning: Unexpected value type for key '{key}'. Skipping.")
  29. continue
  30. return aggregated_params
  31. except Exception as e:
  32. print(f"Error parsing URL: {e}")
  33. return {}
  34. if __name__ == '__main__':
  35. # Example usage
  36. url1 = "https://example.com?param1=value1&param2=value2&param1=value3"
  37. url2 = "https://example.com?param1=value1&param2"
  38. url3 = "https://example.com"
  39. url4 = "invalid_url"
  40. url5 = "https://example.com?param1=value1&param2=value2&param3=value1"
  41. url6 = "https://example.com?param1=value1&param2=value2&param3=value1&param4=value1"
  42. print(f"URL: {url1}, Aggregated: {aggregate_query_strings(url1)}")
  43. print(f"URL: {url2}, Aggregated: {aggregate_query_strings(url2)}")
  44. print(f"URL: {url3}, Aggregated: {aggregate_query_strings(url3)}")
  45. print(f"URL: {url4}, Aggregated: {aggregate_query_strings(url4)}")
  46. print(f"URL: {url5}, Aggregated: {aggregate_query_strings(url5)}")
  47. print(f"URL: {url6}, Aggregated: {aggregate_query_strings(url6)}")

Add your comment