Python Answers

How to retrieve parameters from a URL with Python?

Spread the love

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 = ''
parsed_url = urlparse(url)
captured_value = parse_qs(parsed_url.query)['some_key'][0]


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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *