Skip to content

Instantly share code, notes, and snippets.

@hamletbatista
Last active March 12, 2019 21:24
Show Gist options
  • Save hamletbatista/bf0d3b01369fab342c1d1e6fb6dbc4eb to your computer and use it in GitHub Desktop.
Save hamletbatista/bf0d3b01369fab342c1d1e6fb6dbc4eb to your computer and use it in GitHub Desktop.
#decode URL-econded URL
url_source="/url?sa=t&source=web&rct=j&url=https://support.google.com/webmasters/answer/35291%3Fhl%3Den&ved=2ahUKEwi42-aIwP3gAhUNON8KHf4EB-QQFjAIegQIChAB"
u = urlsplit(url_source)
print(u.query)
#Output -> 'sa=t&source=web&rct=j&url=https://support.google.com/webmasters/answer/35291%3Fhl%3Den&ved=2ahUKEwi42-aIwP3gAhUNON8KHf4EB-QQFjAIegQIChAB'
#note the parameter 'url' is URL encoded because it includes a query string
url_params = parse_qs(u.query)
print(url_params["url"])
#Output -> ['https://support.google.com/webmasters/answer/35291?hl=en']
#in case the URL is already encoded
encoded_url='https://support.google.com/webmasters/answer/35291%3Fhl%3Den'
print(unquote(encoded_url))
#Output -> 'https://support.google.com/webmasters/answer/35291?hl=en'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment