1

For some reason when I try to get and process the following url with python-requests I receive an error causing my program to fail. Other similar urls seems to work fine

import requests

test = requests.get('http://t.co/Ilvvq1cKjK')
print test.url, test.status_code

What could be causing this URL to fail instead of just producing a 404 status code?

2
  • Isn't HTTPConnectionPool(host='nasa.gov', port=80): Max retries exceeded with url: /aeronautics enough info.? The actual connection to the server is being refused so you can't get a 404 from it... Commented Sep 22, 2014 at 1:56
  • That is certainly enough info if I was just processing a single web request, however I am running multiple urls through requests.get() and this one causes the program to terminate is there a way around that? Commented Sep 22, 2014 at 1:59

1 Answer 1

2

The requests library has an exception hierarchy as listed here

So wrap your GET request in a try/except block:

import requests

try:
    test = requests.get('http://t.co/Ilvvq1cKjK')
    print test.url, test.status_code
except requests.exceptions.ConnectionError as e:
    print e.request.url, "*connection failed*"

That way you end up with similar behaviour to what you're doing now (so you get the redirected url), but cater for not being able to connect rather than print the status code.

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

Comments

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.