Skip to content

Commit 14ba030

Browse files
author
jack1q
committed
Species status | species_status.py | added files
1 parent d6423b7 commit 14ba030

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Species-Status/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Species Status
2+
Simple python script that prompts user to enter a species name and returns the species' IUCN category
3+
(extinct, extinct in the wild, critically endangered, endangered, vulnerable, near threatened, least concern) using beautifulsoup. Data is scraped from Wikipedia.

Species-Status/species_status.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Given a species name, this script returns the IUCN category/status of the species
2+
from bs4 import BeautifulSoup
3+
import requests
4+
5+
def get_soup():
6+
species_name = input('Enter the name of the species: ')
7+
result = requests.get(f'https://en.wikipedia.org/wiki/{species_name}')
8+
src = result.content
9+
soup = BeautifulSoup(src, 'lxml')
10+
return soup
11+
12+
def get_status():
13+
soup = get_soup()
14+
try:
15+
species_name = soup.select_one('.mw-redirect[href="/wiki/Least_Concern"]').text
16+
except AttributeError:
17+
try:
18+
species_name = soup.select_one('a[href="/wiki/Vulnerable_species"]').text
19+
except AttributeError:
20+
try:
21+
species_name = soup.select_one('.mw-redirect[href="/wiki/Near_Threatened"]').text
22+
except AttributeError:
23+
try:
24+
species_name = soup.select_one('a[href="/wiki/Endangered_species"]').text
25+
except AttributeError:
26+
try:
27+
species_name = soup.select_one('.mw-redirect[href="/wiki/Critically_endangered_species"]').text
28+
except AttributeError:
29+
try:
30+
species_name = soup.select_one('.mw-redirect[href="/wiki/Extinct_in_the_Wild"]').text
31+
except AttributeError:
32+
try:
33+
species_name = soup.select_one('a[href="/wiki/Extinction"]').text
34+
except AttributeError:
35+
species_name = 'Could not find status... Check spelling or perhaps try the scientific name'
36+
return species_name
37+
print(f'Status:\n> {get_status()}')

0 commit comments

Comments
 (0)