python - Parse value from string -
i have:
url = 'http://example.com/json?key=12345&lat=52.370216&lon=4.895168&status=upcoming&radius=20&offset=0'
how can parse value 20
parameter radius
?
i think not possible urlparse.parse_qs()
, isn't it? there better way rather using regex?
yes, use parse_qs():
parse query string given string argument (data of type application/x-www-form-urlencoded). data returned dictionary. dictionary keys unique query variable names , values lists of values each name.
>>> urlparse import parse_qs >>> url = 'http://example.com/json?key=12345&lat=52.370216&lon=4.895168&status=upcoming&radius=20&offset=0' >>> parse_qs(url)['radius'][0] '20'
upd: @danielroseman noted (see comments), should first pass url through urlparse:
>>> urlparse import parse_qs, urlparse >>> parse_qs(urlparse(url).query)['radius'][0] '20'
Comments
Post a Comment