I am scrapping a web site, but sometimes the laptop lost the connection, and I got (obviously) a requests.exceptions.ConnectionError. Which is the right (or most elegant?) way of recover from this error? I mean: I don't want the program to stop, but retry the connection, maybe some seconds later? This is my code, but I got the feeling is not correct:
def make_soup(session,url): try: n = randint(1, MAX_NAPTIME) sleep(n) response = session.get(url) except requests.exceptions.ConnectionError as req_ce: error_msg = req_ce.args[0].reason.strerror print "Error: %s con la url %s" % (eror_msg, url) session = logout(session) n = randint(MIN_SLEEPTIME, MAX_SLEEPTIME) sleep(n) session = login(session) response = session.get(url) soup = BeautifulSoup(response.text) return soup Any ideas?
Note that I need a session to scrap this pages, so, I think that the login (i.e. login again to the site, after a logout) could be cause troubles