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 !

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. Commented Jun 20, 2018 at 15:11

1 Answer 1

6

You can simply use

u.geturl()

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I got to it as you posted your response :)

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.