From 294dd2b04caad259e6ca5535087c0c44654dfbd2 Mon Sep 17 00:00:00 2001 From: ilikecats Date: Mon, 30 Dec 2024 11:12:12 -0800 Subject: [PATCH] More understandable code --- downloader.py | 52 +++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/downloader.py b/downloader.py index 79f922c..9defd56 100755 --- a/downloader.py +++ b/downloader.py @@ -50,35 +50,35 @@ if requests.get(f"https://www.gocomics.com/{comic_id}/").status_code != 200: ### Get date of first comic -first_comic = bs( - requests.get( - "https://www.gocomics.com/{}".format( - bs(requests.get( - f"https://www.gocomics.com/{comic_id}/").content, - "html.parser").find( - "span", string="Read Now" - ).parent.parent.parent.get("href")[1:] - ) - ).content, "html.parser" - ).find(class_="fa-backward").get("href")[-10:] +## Get comic homepage. If status code is not 200 (success), retry. +status = False +while status != 200: + if status != False: + print("Could not get home page. Retrying...") + home_pg = requests.get(f"https://www.gocomics.com/{comic_id}/") + status = home_pg.status_code + +home_pg = bs(home_pg.content, "html.parser") ## Convert to BeautifulSoup + +## Get latest comic +status = False +latest_url = "https://www.gocomics.com/{}".format( + home_pg.find("span", string="Read Now" + ).parent.parent.parent.get("href")[1:]) +while status != 200: + if status != False: + print("Could not get latest comic. Retrying...") + ## Get "Read Now" button's href from home page, and request that + latest_comic = requests.get(latest_url) + status = latest_comic.status_code + +latest_comic = bs(latest_comic.content, "html.parser") ## Convert to BeautifulSoup + +## Get date of first comic +first_comic = latest_comic.find(class_="fa-backward").get("href")[-10:] current_comic = first_comic -""" -That's a big chunk of code. Here's what it does: - -1) Get the comic home page. -2) Get the latest comic (from the "Read Now" button) -3) Get the first comic (from the button with the CSS class "fa-backward") - -The code means: - -Get last 10 chars of the href of the element with the class of "fa-backward" -from the url of the that is the parent(x3) of the span labled "Read Now" -from the home page of the comic id. - -""" - print(f"Start date: {first_comic}") ### Make directory to store comics in