4

As in this post, I attempt to get the final redirect of a webpage as:

import urllib.request
response = urllib.request.urlopen(url)
response.geturl()

But this doesn't work as I get the "HTTPError: HTTP Error 300: Multiple Choices" error when attempting to use urlopen.

See documentation for these methods here.

EDIT:

This problem is different than the Python: urllib2.HTTPError: HTTP Error 300: Multiple Choices question, because they skip the error-causing pages, while I have to obtain the final destination.

Ólavur
  • 410
  • 4
  • 15
  • 2
    Why don’t you use the requests library? It’s much easier for dealing with this kind of situation – Taku Jul 04 '17 at 14:22
  • Possible duplicate of [Python: urllib2.HTTPError: HTTP Error 300: Multiple Choices](https://stackoverflow.com/questions/32641862/python-urllib2-httperror-http-error-300-multiple-choices) – mx0 Jul 04 '17 at 17:21
  • @mx0 That post addresses the same issue, but while they are skipping the redirected pages, I actually have to follow the link and obtain the final destination. – Ólavur Jul 05 '17 at 07:40

1 Answers1

0

As suggested by @abccd, I used the requests library. So I will describe the solution.

import requests

url_base = 'something'  # You need this because the redirect URL is relative.
url = url_base + 'somethingelse'

response = requests.get(url)

# Check if the request returned with the 300 error code.
if response.status_code == 300:
    redirect_url = url_base + response.headers['Location']  # Get new URL.
    response = requests.get(redirect_url)  # Make a new request.
Ólavur
  • 410
  • 4
  • 15