Ordiance

scrapeAbilitiesData

Oct 22nd, 2023
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import pandas as pd
  4.  
  5. # Fetch "Pokémon"-ability information from PokemonDB
  6. URL = "https://pokemondb.net/ability"
  7. response = requests.get(URL)
  8.  
  9. # Initialize parser
  10. soup = BeautifulSoup(response.content, 'html.parser')
  11.  
  12. # Find data-table storing all abilities along with their descriptions
  13. table = soup.find(attrs={"class" : "data-table"})
  14.  
  15. # Store all rows of table in a list, skip header
  16. rows = table.find_all("tr")[1:]
  17.  
  18. # Iterate through all rows, find all cells, store their text contents as a list entry
  19. entries = [[entry.text for entry in row.find_all("td")] for row in rows]
  20.  
  21.  
  22. # Create dataframe from 2d-array "entries"
  23. df = pd.DataFrame(entries, columns=["Name", "Pokémon", "Description", "Generation"])
  24.  
  25. # Drop columns "Pokémon" and "Generation"
  26. df.drop(["Pokémon", "Generation"], axis=1, inplace=True)
  27.  
  28. # Fill out missing description entry of ability "Quick Draw"
  29. df.Description.iloc[185] = "Enables the Pokémon to move first occasionally."
  30.  
  31.  
  32. df.to_csv("dataClean/Abilities.csv")
  33.  
Advertisement
Add Comment
Please, Sign In to add comment