Search code examples
python-3.xpython-requestsapi-design

How to make requests keep trying to connect to url regardless of exception from where it left off in the list?


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)

Solution

  • 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)