In the loop I loop through ip and useragent to parse the site, some of the ip do not respond and after that an exception is thrown, and the cycle goes on, so, how to make it so that after the exception it does not continue further, skipping the place where the exception occurred, and started again from the place where the exception occurred, but with a different ip, because otherwise it misses the page.

def main(): url = 'http://old.toto-info.co/DataService.svc/GetMaxPrizeCoupons' useragents = open("useragents.txt").read().split('\n') proxies = open("proxies.txt").read().split('\n') for i in range(0,200,20): try: a = (uniform(1, 2)) sleep(a) StartFroms = i useragent = "'" + choice(useragents) + "'" proxy = {'http': 'http://' + choice(proxies)} get_html(url,useragent,proxy,StartFroms) except: print("not working") if __name__ == '__main__': main() 

    2 answers 2

    In the case of an exception, it is necessary to start the for loop from the point where it occurred:

     start = 0 # переменная для хранения точки входа в for while start < finish: try: for i in range(start, finish, step): do_something() if i >= finish - step: start = finish # для выхода из внешнего цикла except: start = i # перезапускаем for c точки исключения 

    Example:

     from random import randint start = 0 finish = 4 while start < finish: try: for i in range(start, finish): print i if randint(0, 1): raise ValueError(i) if i >= finish - 1: start = finish except ValueError as e: print "exception:", e start = i 

    Conclusion:

     D:\Python\python.exe D:/PycharmProjects/tl/aa.py 0 exception: 0 0 exception: 0 0 1 2 3 exception: 3 3 Process finished with exit code 0 
    • no, did not work @ andy.37 - Pasha Vasilyev
    • @ PashaVasilyev What does "not work" mean? I added a part of the correct exit from the cycle there and an example - andy.37
    • @PashaVasilyev, sorry, of course it was wrong, corrected - andy.37
    • I apologize, did not immediately notice what has changed, everything is working as it should now@andy.37 - Pasha Vasilyev
    • @PashaVasilyev, yes, I hurried myself, so soon I should apologize :) - andy.37

    or use recursion

     def main(_i=0): for i in range(_i,200,20): try: pass except: return main(i) # продолжить с ошибочного индекса 

    or

     def proxy_get_html(url, useragent, StartFroms): proxy = {'http': 'http://' + choice(proxies)} try: get_html(url, useragent, proxy, StartFroms) except: return proxy_get_html(url, useragent, StartFroms) else: return True def main(): for i in range(0,200,20): proxy_get_html(url, useragent, proxy, StartFroms)