0

I am trying to get data from an website and I am with an issue that I can't pass trough an array.

I need to run requests.get(value[0]), requests.get(value[1]), requests.get(value[N]). But when I got an error this loop just STOP.

The code:

import requests

value = ['http://teste', 'http://cast4.audiostream.com.br:8651/status-json.xsl']

try:
  for item in value:
      response = requests.get(item)
      print(response)
except:
      print('response')

And those two URLs are a real case, if I run this script will explode an exception, but the second URL is a valid URL, I've tried some other options using while, putting pass instead of print() but without success, how can I handle that to let my loop for, keep doing its job??

Thanks!!!

3
  • I need to pass trough all items from an array, the real error is that test do not exists, and this code do not allowing me to continue to run that. Commented Nov 18, 2019 at 19:35
  • Maybe you mean to have the try...except clauses in the loop, instead of around it. But your question isn't completely clear to me either... Commented Nov 18, 2019 at 19:35
  • requests.exceptions.ConnectionError: HTTPConnectionPool(host='teste', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f4f840886a0>: Failed to establish a new connection: [Errno -2] Name or service not known')) Commented Nov 18, 2019 at 19:36

3 Answers 3

2

I think it's because you are puting the try statement outside the for loop. Changing the code to this may work:

import requests

value = ['http://teste', 'http://cast4.audiostream.com.br:8651/status-json.xsl']
for item in value:
    try:
        response = requests.get(item)
        print(response)
    except:
         print('response')
Sign up to request clarification or add additional context in comments.

Comments

1

Switch the places of for and try, like:

for item in value:
  try:
      response = requests.get(item)
      print(response)
  except:
      print('response')

Also, avoid using bare excepts. Observe what error the code is raising, then change your except to catch that exception, e.g:

for item in value:
  try:
      response = requests.get(item)
      print(response)
  except requests.exceptions.Timeout:
      print('timeout exception')

Add more exceptions by new exceptions you get. Or if you don't want your program to stop when you encounter a new exception, at least print the error:

except Exception as error:
  print(error)

1 Comment

You got that, I changed try position, and added ``` except Exception as error: print(error) ``` and it works!! Thank you =)
1

You're missing the ".com" in your first URL

import requests

value = ['http://teste', 'http://cast4.audiostream.com.br:8651/status-json.xsl']

for item in value:
    try:
        response = requests.get(item)
        print(response)
    except:
        print('Error')

3 Comments

It's almost there, I just added '.com' at the end of 'teste' but, I was looking for some way to fix that without this kind of solution, because if the link is broken, I need to keep doing my loop for without handle any part of my link, just adding on database saying "Oh no, this link is wrong, please fix it".
Oh gotcha, you just need to adjust the position of your try for that
Basically try to get this URL if it works fine, if not throw an error and continue to the next URL in the list

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.