Created
November 28, 2021 18:10
-
-
Save zperk13/b4d57953475cb7e1fbce0cead9e386f8 to your computer and use it in GitHub Desktop.
Find out how closely related two animals are in the Scientific Classification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I wrote this code a long time ago lol | |
import requests | |
import re | |
from pprint import pprint | |
class Animal(object): | |
def __init__(self, name): | |
self.name = name | |
self.url_1 = f'https://en.wikipedia.org/wiki/{name.replace(" ", "_")}' | |
self.r = requests.get(self.url_1) | |
if not self.r.ok: | |
self.r = requests.get( | |
f'https://en.wikipedia.org/w/index.php?search={self.name.replace(" ", "%20")}&ns0=1').text | |
self.r = re.findall(r'<a.+?/a>', self.r) | |
self.r = self.r[['ask for it to be created' in x for x in self.r].index(True) + 1] | |
self.r = re.findall(r'href=".+?"', self.r)[0][6:-1] | |
if self.r.startswith('/'): | |
self.r = 'https://en.wikipedia.org' + self.r | |
print(f'{self.url_1} not found. Attempting {self.r}') | |
self.r = requests.get(self.r) | |
else: | |
print(self.url_1) | |
self.r = self.r.text | |
self.classification = {} | |
ranks = [x[4:-7] for x in re.findall(r'<td>.+?:\n</td>', re.findall(r'<table class="infobox.+?">[^我]+?</tbody>', self.r)[0])] | |
self.classification = {'order': ranks, 'ranks': {}} | |
for rank in ranks: | |
rank_name = re.findall(r'>.+?</[abi]>', re.findall(r'<[abi].+?</[abi]>', re.findall(fr'<tr>\s*?<td>{rank}:.*?\s*?</td>\s*?<td>.*?<[abi].*?>.+?</[abi]>', self.r)[0])[0])[0][1:-4].replace('<i>', '') | |
self.classification['ranks'][rank] = rank_name | |
def get_classification(name): | |
animal = Animal(name) | |
return animal.classification | |
animals = [Animal(input('Please type in the name of the first animal: ')), Animal(input('Please type in the name of the second animal: '))] | |
pprint([x.classification['ranks'] for x in animals]) | |
shared_order = [x for x in animals[0].classification['order'] if x in animals[1].classification['order']] | |
print('\n\n') | |
print(shared_order) | |
print('\n') | |
shared_ranks = [] | |
for rank in animals[0].classification['order']: | |
if rank in shared_order: | |
if animals[0].classification['ranks'][rank] == animals[1].classification['ranks'][rank]: | |
shared_ranks.append(animals[0].classification['ranks'][rank]) | |
if len(shared_ranks) != 0: | |
print(shared_ranks[-1]) | |
else: | |
print('These things are not related') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment