Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

How do I save BeautifulSoup web scraped table to csv file

$
0
0

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']

Viewing all articles
Browse latest Browse all 13891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>