I web scraped the contents of a table using this method and now I am looking to save it into a csv file but I am not sure how (it's for a project in class so I can't post any of my actual code because I might get flagged for plagiarism but I did exactly the same thing as the example). I am new to data science/python so it might not have been the best method but it was the one I could figure out how to actually scrape the data. Thank you!!
https://scrapfly.io/blog/how-to-scrape-tables-with-beautifulsoup/
from bs4 import BeautifulSoupimport requests soup = BeautifulSoup(requests.get("https://www.w3schools.com/html/html_tables.asp").text)# first we should find our table object:table = soup.find('table', id="customers")# then we can iterate through each row and extract either header or row values:header = []rows = []for i, row in enumerate(table.find_all('tr')): if i == 0: header = [el.text.strip() for el in row.find_all('th')] else: rows.append([el.text.strip() for el in row.find_all('td')])print(header)['Company', 'Contact', 'Country']for row in rows: print(row)['Alfreds Futterkiste', 'Maria Anders', 'Germany']['Centro comercial Moctezuma', 'Francisco Chang', 'Mexico']['Ernst Handel', 'Roland Mendel', 'Austria']['Island Trading', 'Helen Bennett', 'UK']['Laughing Bacchus Winecellars', 'Yoshi Tannamuri', 'Canada']['Magazzini Alimentari Riuniti', 'Giovanni Rovelli', 'Italy']