def try_except_function(self, *args, **kwargs): for i in range(10): try: results = func(self, *args, **kwargs) return results except (ClientError, Exception) as e: Crawler.log_error(e) if 'Not Found' in str(e): raise ValueError('Профиль не найден') time.sleep(5) continue
def __request_json(self, url): for i in range(5): try: query = requests.get(url, headers=self.get_headers) if query.status_code == 200: return ujson.loads(query.text) elif query.status_code == 403 or query.status_code == 429: logging.warning( f'Ошибка {query.status_code}. Возможно, невалидный токен' ) self.guest_token = self.get_tokens() else: raise RequestError( f'Ошибка {query.status_code} при запросе JSON') except RequestError as e: time.sleep(self.delay_after_request_error) Crawler.log_error(e) except (URLError, ConnectionError, Exception) as e: time.sleep(self.delay_after_request_error) Crawler.log_error(e) raise GetInfoError('Не удается получить информацию об источнике')