From e881c24f707bf29e78802fe79a25d594782b3b71 Mon Sep 17 00:00:00 2001 From: "Skylar \"The Cobra\" Widulski" Date: Sat, 21 Jan 2023 00:38:58 -0500 Subject: [PATCH] Don't explode upon 404 Signed-off-by: Skylar "The Cobra" Widulski --- main.py | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/main.py b/main.py index 6a2e34a..0790b72 100644 --- a/main.py +++ b/main.py @@ -13,29 +13,26 @@ def scrape(url, arg=None): else: data = requests.get(f"{url}{arg}") - if data.status_code == 200: - our_path = re.sub(r".*://.*/", "/", request.url) - path = re.sub(r".*://.*/", "/", data.url) - print(our_path, path) - if our_path != path: - return f"REDIRECT {path}" - ret = [] - soup = BeautifulSoup(data.text, "html.parser") - for div in soup.find_all("div"): - defid = div.get('data-defid') - if defid != None: - definition = soup.find(attrs={"data-defid": [defid]}) - word = definition.select("div div h1 a, div div h2 a")[0].text - meaning = definition.find(attrs={"class" : ["break-words meaning mb-4"]}).decode_contents() - example = definition.find(attrs={"class" : ["break-words example italic mb-4"]}).decode_contents() - contributor = definition.find(attrs={"class" : ["contributor font-bold"]}) - ret.append([defid, word, meaning, example, contributor]) - pages = soup.find(attrs={"class" : ["pagination text-xl text-center"]}) - if pages == None: - pages = "" - return (ret, pages) - else: - return f"Couldn't get data from Urban Dictionary\n{data.status_code}" + our_path = re.sub(r".*://.*/", "/", request.url) + path = re.sub(r".*://.*/", "/", data.url) + print(our_path, path) + if our_path != path: + return f"REDIRECT {path}" + ret = [] + soup = BeautifulSoup(data.text, "html.parser") + for div in soup.find_all("div"): + defid = div.get('data-defid') + if defid != None: + definition = soup.find(attrs={"data-defid": [defid]}) + word = definition.select("div div h1 a, div div h2 a")[0].text + meaning = definition.find(attrs={"class" : ["break-words meaning mb-4"]}).decode_contents() + example = definition.find(attrs={"class" : ["break-words example italic mb-4"]}).decode_contents() + contributor = definition.find(attrs={"class" : ["contributor font-bold"]}) + ret.append([defid, word, meaning, example, contributor]) + pages = soup.find(attrs={"class" : ["pagination text-xl text-center"]}) + if pages == None: + pages = "" + return (ret, pages) def render(data): return render_template('index.html', data=data)