Using Python how can I print request_token
from a URL like:
https://kite.trade/?request_token=p87tOTSXRSp4O20TGr870n2JiXFKISIh&action=login&status=success
IE: the text between =
to &
following request_token
.
Using Python how can I print request_token
from a URL like:
https://kite.trade/?request_token=p87tOTSXRSp4O20TGr870n2JiXFKISIh&action=login&status=success
IE: the text between =
to &
following request_token
.
I think what you're looking for is parse_qs
from urllib.parse
.
from urllib.parse import urlparse, parse_qs #import stuff
#parse the url
url_obj = urlparse('https://kite.trade/?request_token=p87tOTSXRSp4O20TGr870n2JiXFKISIh&action=login&status=success')
#get a dictionary from the query
q_dict = parse_qs(url_obj.query)
#now get query args by key name.
print(q_dict["request_token"])
print(q_dict["status"])
Use the urlparse library in python 2 https://docs.python.org/2/library/urlparse.html
Or urllib.parse in python 3 https://docs.python.org/3/library/urllib.parse.html
You can use split the strings using string functions and put the query into a dict like:
url = 'https://kite.trade/?request_token=p87tOTSXRSp4O20TGr870n2JiXFKISIh&' \
'action=login&status=success'
query = dict(a.split('=') for a in url.split('?')[1].split('&'))
print(query)
You can do the same using the urllib like:
import urllib
query = urllib.parse.parse_qs(urllib.parse.urlparse(url).query)
print(query['request_token'][0])
{'request_token': 'p87tOTSXRSp4O20TGr870n2JiXFKISIh', 'action': 'login', 'status': 'success'}
p87tOTSXRSp4O20TGr870n2JiXFKISIh