#!/usr/bin/env python from bs4 import BeautifulSoup import requests import sys import re ######## Get Pokemon Name ######## if len(sys.argv) < 2: pokemon = input("Enter the name of the pokemon to look up: ").title() # Two Word Names Like Mime Jr. elif len(sys.argv) == 3: pokemon = (sys.argv[1] + " " + sys.argv[2]).title() else: pokemon = sys.argv[1].title() if pokemon == "mr-mime": pokemon = "Mr.Mime" #serebii listed this way elif pokemon == "ho-oh": pokemon = "Ho-oh" #serebii listed this way ######## Get Pokedex Entry Number ######## begin_url = ["http://serebii.net/pokedex-xy/", "http://serebii.net/pokedex-sv/", "http://serebii.net/pokedex-swsh/", "http://serebii.net/pokedex-sm/", "http://serebii.net/pokedex-bw/", "http://serebii.net/pokedex-dp/", "http://serebii.net/pokedex-rs/", "http://serebii.net/pokedex-gs/", "http://serebii.net/pokedex/"] for i in begin_url: try: request1 = requests.get(i) except OSError: sys.exit(1) html1 = request1.content soup1 = BeautifulSoup(html1, "html.parser") page_info1 = soup1.get_text() # Pokedex Entries Start At Index 5000 pokemon_index = page_info1.find(pokemon, 5000) if pokemon_index != -1: break if pokemon_index == -1: print("You didn't enter a valid pokemon!") sys.exit(1) pokedex_number = page_info1[pokemon_index-4 : pokemon_index-1] ######## Get Pokemon Info ######## for i in begin_url: try: full_url = i + pokedex_number + ".shtml" request2 = requests.get(full_url) except OSError: sys.exit(1) html2 = request2.content soup2 = BeautifulSoup(html2, "html.parser") page_info2 = soup2.get_text() page_not_found = bool(soup2.find(string=re.compile("Page Not Found"))) if page_not_found == False: break if page_not_found: print(" Stats currently unavailable :( ") sys.exit(1) ######## Get Stats ######## base_total = soup2.find(string=re.compile("Base Stats - Total:")) # Only want everything after Total base_total = base_total[base_total.find("Total"):] base_stats = soup2.find_all(class_="fooinfo") hp = str(base_stats[len(base_stats)-21]) attack = str(base_stats[len(base_stats)-20]) defence = str(base_stats[len(base_stats)-19]) spec_attack = str(base_stats[len(base_stats)-18]) spec_defence = str(base_stats[len(base_stats)-17]) speed = str(base_stats[len(base_stats)-16]) hp = hp[hp.find(">")+1 : hp.find("/")-1] attack = attack[attack.find(">")+1 : attack.find("/")-1] defence = defence[defence.find(">")+1 : defence.find("/")-1] spec_attack = spec_attack[spec_attack.find(">")+1 : spec_attack.find("/")-1] spec_defence = spec_defence[spec_defence.find(">")+1 : spec_defence.find("/")-1] speed = speed[speed.find(">")+1:speed.find("/")-1] ######## Get Abilities ######## abilities_index = page_info2.find("Abilities:") abilities = page_info2[abilities_index:] end_index = abilities.find("\n") abilities = abilities[:end_index-1] ######## Get Gender ######## gender_index = page_info2.find("GenderType") gender = page_info2[gender_index:] end_index = gender.find("\n") gender = gender[:end_index] gender = gender[gender.find(" ")+1:] if "is Genderless" in gender: gender = "Genderless" ######## Print everything out ######## print("") print("Name: ", pokemon) print("Pokedex Number: ", pokedex_number) print("Gender Ratio: ", str(gender)) print(abilities) print("") print("Base Stats") print("----------------------") print(base_total) print("Hp: ", hp) print("Attack: ", attack) print("Defence: ", defence) print("Special Attack: ", spec_attack) print("Special Defence: ", spec_defence) print("Speed: ", speed)