mardi 30 mai 2017

Redirect handling Python 3

I am writing a program to open and read webpages and do stuff with the stuff on the page. I am using urllib.request package for that. This is Python 3.4.3. I want to make sure my code can operate with redirect requests. Right now my code just catches redirects and prints out that there was an HTTPError and does nothing. I would want it to actually redirect to the new page in case of a redirect request and read that page instead. My code currently is this:

try:
        text = str(urllib.request.urlopen(url, timeout=10).read())
except ValueError as error:
        print(error)
except urllib.error.HTTPError as error:
        print(error)
except urllib.error.URLError as error:
        print(error)
except timeout as error:
        print(error)

Please help me I am new to this. Thanks!




Aucun commentaire:

Enregistrer un commentaire