1

Lets say this is my URL string:

https://stackexchange.com/oauth/login_success/#access_token=xxxxxx))&expires=86400

I want to parse just the access token part "xxxxxx))"

This is what I've done so far:

from urllib.parse import urlparse
strr = "https://stackexchange.com/oauth/login_success/#access_token=xxxxxx))&expires=86400"

o = urlparse(strr)

print(o.fragment)

The output I'm getting is:

access_token=xxxxxx))&expires=86400

What's the best way from here to get only the "xxxxxx))" part? Use regex?

1
  • 1
    pydoc3 urllib.parse.parse_qs Commented Apr 4, 2016 at 21:06

2 Answers 2

3

Here is one way to do that - first use urlparse() and then call parse_qs() on the fragment:

>>> from urllib.parse import parse_qs, urlparse
>>> 
>>> strr = "https://stackexchange.com/oauth/login_success/#access_token=xxxxxx))&expires=86400"
>>> o = parse_qs(urlparse(strr).fragment)
>>> print(o['access_token'])
['xxxxxx))']
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a regular expression:

(?<=access_token=)[^\)]*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.