To retrieve parameters from a URL with Python, we use urlparse
from urllib.parse
.
For instance, we write
from urllib.parse import urlparse
from urllib.parse import parse_qs
url = 'https://www.example.com/some_path?some_key=some_value'
parsed_url = urlparse(url)
captured_value = parse_qs(parsed_url.query)['some_key'][0]
print(captured_value)
to call parse_qs
on the parsed_url
object returned by urlparse
to get the query string.
And then we get the query value from the dict returned by parse_qs
.
parsed_url.query
has the query string of the URL.