scrap & csv
How to scrap and export results in csv with python?
You can achieve this by using Python and some libraries like BeautifulSoup for web scraping and Pandas for data manipulation and saving to a CSV file. Here's a step-by-step guide on how to do it:
import requests
from bs4 import BeautifulSoup
import pandas as pd
# Send an HTTP GET request to the webpage
url = "https://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_by_National_Pok%C3%A9dex_number"
response = requests.get(url)
# Parse the HTML content of the page using BeautifulSoup
soup = BeautifulSoup(response.text, 'html.parser')
# Find the table containing the data
table = soup.find('table', {'class': 'roundy'})
# Initialize lists to store the data
ndex_list = []
pokemon_list = []
type_list = []
# Iterate through the rows of the table
for row in table.find_all('tr')[2:]: # Skip the first two header rows
columns = row.find_all('td')
# Check if there are enough columns to extract data
if len(columns) >= 5:
ndex = columns[0].text.strip()
name = columns[3].text.strip()
types = [t.text.strip() for t in columns[4].find_all('a')]
ndex_list.append(ndex)
pokemon_list.append(name)
type_list.append('/'.join(types))
# Create a DataFrame from the lists
data = {
'National Dex Number': ndex_list,
'Pokemon': pokemon_list,
'Type': type_list
}
df = pd.DataFrame(data)
# Save the data to a CSV file
df.to_csv('pokemon_data.csv', index=False)
print("Data has been scraped and saved to pokemon_data.csv")
This script will scrape the National Dex number, Pokemon names, and types from the given URL and save the data to a CSV file named "pokemon_data.csv" in the same directory as your script.
You can find the result here.