Home > Software engineering >  Get Parameter passed from query string in python
Get Parameter passed from query string in python

Time:01-03

I'm calling this url by passing query string parameter, i have to pass name parameter to details.py

Url is :

http://www.somesite.com/details.py?name=test

i tried using request but i can't get this parameter.

Code:

import requests
resp = requests.get('name')

CodePudding user response:

requests is for performing http calls

To "get" the value from that string, you'd want urllib.parse module for parsing URIs

Getting a query parameter on the server-side would depend on what web framework you're using

CodePudding user response:

Use urllib to parse urls;

from urllib.parse import urlparse
from urllib.parse import parse_qs

url = 'http://www.somesite.com/details.py?name=test'

# parse the url 
parsed_url = urlparse(url)

# parse the request's parameters
parsed_params = parse_qs(parsed_url.query)

# get the 'name' parameter 
name = parsed_params['name'][0]

print(name)
>>> 'test'
  • Related