Skip to content

Instantly share code, notes, and snippets.

@adgedenkers
Last active February 2, 2024 22:56
Show Gist options
  • Save adgedenkers/84ba40c48d3c6a3c57edc4282c219bad to your computer and use it in GitHub Desktop.
Save adgedenkers/84ba40c48d3c6a3c57edc4282c219bad to your computer and use it in GitHub Desktop.
Fully Functional Web Scraper for Pokemon Information
'''
File: pokemon.py
Project: resources
Created: 2024-02-02
Updated: 2024-02-02
Author: Adge Denkers
https://github.com/adgedenkers/
Versoin: 0.1.0
License: MIT License
'''
import requests
from bs4 import BeautifulSoup
import argparse
class Pokemon:
def __init__(self, number) -> None:
self._number = number
self._name = None
self.load_pokemon_data()
def load_pokemon_data(self):
number = self._number
url = f'https://pokemon.gameinfo.io/en/pokemon/{number}'
response = requests.get(url)
if response.ok:
soup = BeautifulSoup(response.text, 'html.parser')
self._name = soup.find('div', class_='title').find('h1').text.strip()
self._types = [type_tag.text for type_tag in soup.select(".pokemon-type .large-type .type.large")]
self.weaknesses_list, self.resistances_list = self._get_weaknesses_and_resistances(soup)
else:
pass
def _get_weaknesses_and_resistances(self, soup):
# Implement the logic for extracting weaknesses and resistances here
# --- start of getting weaknesses ---
weaknesses_table = soup.find('table', {'class': 'weaknesses weak'})
# List to hold the weaknesses in the desired format
weaknesses_list = []
if weaknesses_table:
# Find all <tr> elements in the table
rows = weaknesses_table.find_all('tr')
for row in rows:
# Extract type and damage information from each row
type_info = row.find('a', {'class': 'type'}).text.strip().lower() # Convert type to lowercase
damage_info = row.find('span').text.strip('%') # Remove '%' from the damage information
try:
# Convert damage_info to integer
damage_info = int(damage_info)
except ValueError:
# If conversion fails, set damage_info to None or handle it as appropriate
damage_info = None
# Append the formatted weakness as a list [type, damage] to the weaknesses_list
weaknesses_list.append([type_info, damage_info])
# --- end of getting weaknesses ---
# --- start of getting resistances ---
resistances_table = soup.find('table', {'class': 'weaknesses res'})
# List to hold the resistances in the desired format
resistances_list = []
if resistances_table:
# Find all <tr> elements in the table
rows = resistances_table.find_all('tr')
for row in rows:
# Extract type and damage information from each row
type_info = row.find('a', {'class': 'type'}).text.strip().lower() # Convert type to lowercase
damage_info = row.find('span').text.strip('%') # Remove '%' from the damage information
try:
# Convert damage_info to integer
damage_info = int(damage_info)
except ValueError:
# If conversion fails, set damage_info to None or handle it as appropriate
damage_info = None
# Append the formatted resistance as a list [type, damage] to the resistances_list
resistances_list.append([type_info, damage_info])
# --- end of getting resistances ---
return weaknesses_list, resistances_list
@property
def number(self):
return self._number
@property
def name(self):
return self._name
def main(number):
pokemon = Pokemon(number)
pokemon.basic_info()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Get basic Pokémon information.')
parser.add_argument('number', type=int, help='Pokémon number')
args = parser.parse_args()
main(args.number)
# Useage Examples
# $ python get_pokemon_info.py 25
# >>> 25 :: Pikachu (Electric)
# from get_pokemon_info import Pokemon
# pokemon = Pokemon(25)
# pokemon.basic_info()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment