1

I have a list of IDs that I am passing into a URL within a for loop:

L = [1,2,3]
lst=[]
for i in L:
    url = 'URL.Id={}'.format(i)
    xml_data1 = requests.get(url).text
    lst.append(xml_data1)
    time.sleep(1)
    print(xml_data1)

I am trying to create a try/catch where regardless of the error, the request library keeps trying to connect to the URL from the ID it left off on from the list (L), how would I do this?

I setup this try/catch from this answer (Correct way to try/except using Python requests module?)

However this forces the system to exit.

try:
    for i in L:
        url = 'URL.Id={}'.format(i)
        xml_data1 = requests.get(url).text
        lst.append(xml_data1)
        time.sleep(1)
        print(xml_data1)
except requests.exceptions.RequestException as e:
    print (e)
    sys.exit(1)
RustyShackleford
  • 3,462
  • 9
  • 40
  • 81

1 Answers1

1

You can put the try-except block in a loop and only break the loop when the request does not raise an exception:

L = [1,2,3]
lst=[]
for i in L:
    url = 'URL.Id={}'.format(i)
    while True:
        try:
            xml_data1 = requests.get(url).text
            break
        except requests.exceptions.RequestException as e:
            print(e)
    lst.append(xml_data1)
    time.sleep(1)
    print(xml_data1)
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • This is beautiful thank you so much, but how does this keep trying to reconnect? – RustyShackleford Aug 30 '18 at 16:51
  • It does so with an infinite `while` loop that only breaks when `requests.get` doesn't raise an exception. If an exception is raised, then it prints the exception and goes into the next iteration of the `while` loop, which makes a request again. – blhsing Aug 30 '18 at 17:00