Redirection url using urllib in Python 3
Asked Answered
H

1

6

I would need to know what is my final URL when following redirections using urllib in Python 3.

Let's say I've some code like :

opener = urllib.request.build_opener()
request = urllib.request.Request(url)
u = opener.open(request)

If my urls redirects to another website, how can I know this new website URL ? I've found nothing useful in documentation.

Thanks for your help !

Hickey answered 9/2, 2011 at 14:34 Comment(1)
urllib.request.urlopen(url) is the updated version and was all that was necessary for me, plus an exception handler for errors like 404.Dichroite
D
6

You can simply use

u.geturl()

to get the URL you were redirected to (or the original one if no redirect happened).

Devinna answered 9/2, 2011 at 14:40 Comment(1)
Thanks, I got to it as you posted your response :)Hickey

© 2022 - 2024 — McMap. All rights reserved.