From f24b8ae4ee5abd83bbd07e5acaced72421c2026e Mon Sep 17 00:00:00 2001 From: Ching Date: Fri, 11 Jun 2021 10:28:13 +0800 Subject: [PATCH] [M]add 403 retry --- jandan_toot.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/jandan_toot.py b/jandan_toot.py index 9dbc286..9327075 100644 --- a/jandan_toot.py +++ b/jandan_toot.py @@ -65,9 +65,15 @@ def toot(proxy=None): while page_count and jandan_pic_url: resp = session.get(jandan_pic_url, timeout=2) - if resp.status_code != 200: + retry_count = 5 + if resp.status_code != 200 and retry_count: + proxy = get_proxy().get('proxy') + session.proxies.update({"https": "https://{}".format(proxy)}) time.sleep(10) + retry_count -= 1 continue + if retry_count <= 0 and resp.status_code != 200: + raise ValueError('http status not 200') soup = BeautifulSoup(resp.content, 'html.parser') jandan_pic_url = soup.find(class_='previous-comment-page')['href'] page_count -= 1