forked from cobra/rural-dict
Don't explode upon 404
Signed-off-by: Skylar "The Cobra" Widulski <cobra@vern.cc>
This commit is contained in:
parent
3f99b3b6c2
commit
e881c24f70
43
main.py
43
main.py
|
@ -13,29 +13,26 @@ def scrape(url, arg=None):
|
||||||
else:
|
else:
|
||||||
data = requests.get(f"{url}{arg}")
|
data = requests.get(f"{url}{arg}")
|
||||||
|
|
||||||
if data.status_code == 200:
|
our_path = re.sub(r".*://.*/", "/", request.url)
|
||||||
our_path = re.sub(r".*://.*/", "/", request.url)
|
path = re.sub(r".*://.*/", "/", data.url)
|
||||||
path = re.sub(r".*://.*/", "/", data.url)
|
print(our_path, path)
|
||||||
print(our_path, path)
|
if our_path != path:
|
||||||
if our_path != path:
|
return f"REDIRECT {path}"
|
||||||
return f"REDIRECT {path}"
|
ret = []
|
||||||
ret = []
|
soup = BeautifulSoup(data.text, "html.parser")
|
||||||
soup = BeautifulSoup(data.text, "html.parser")
|
for div in soup.find_all("div"):
|
||||||
for div in soup.find_all("div"):
|
defid = div.get('data-defid')
|
||||||
defid = div.get('data-defid')
|
if defid != None:
|
||||||
if defid != None:
|
definition = soup.find(attrs={"data-defid": [defid]})
|
||||||
definition = soup.find(attrs={"data-defid": [defid]})
|
word = definition.select("div div h1 a, div div h2 a")[0].text
|
||||||
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()
|
||||||
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()
|
||||||
example = definition.find(attrs={"class" : ["break-words example italic mb-4"]}).decode_contents()
|
contributor = definition.find(attrs={"class" : ["contributor font-bold"]})
|
||||||
contributor = definition.find(attrs={"class" : ["contributor font-bold"]})
|
ret.append([defid, word, meaning, example, contributor])
|
||||||
ret.append([defid, word, meaning, example, contributor])
|
pages = soup.find(attrs={"class" : ["pagination text-xl text-center"]})
|
||||||
pages = soup.find(attrs={"class" : ["pagination text-xl text-center"]})
|
if pages == None:
|
||||||
if pages == None:
|
pages = ""
|
||||||
pages = ""
|
return (ret, pages)
|
||||||
return (ret, pages)
|
|
||||||
else:
|
|
||||||
return f"Couldn't get data from Urban Dictionary\n{data.status_code}"
|
|
||||||
|
|
||||||
def render(data):
|
def render(data):
|
||||||
return render_template('index.html', data=data)
|
return render_template('index.html', data=data)
|
||||||
|
|
Loading…
Reference in New Issue