More understandable code

This commit is contained in:
ilikecats
2024-12-30 11:12:12 -08:00
parent ad7328e2b1
commit 294dd2b04c
+26 -26
View File
@@ -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 <a> 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