-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcsv_write_example.py
More file actions
32 lines (28 loc) · 1.03 KB
/
csv_write_example.py
File metadata and controls
32 lines (28 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# create a new CSV file from a table on a web page
import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("https://weimergeeks.com/examples/scraping/example1.html")
bsObj = BeautifulSoup(html, "html.parser")
# open new file for writing -
csvfile = open("example.csv", 'w', newline='', encoding='utf-8')
# make a new variable, c, for Python's CSV writer object -
c = csv.writer(csvfile)
# there is only one table on this page - get it
table = bsObj.find("table")
# get all rows from table
tr_list = table.findAll("tr")
# loop over all the rows
for row in tr_list:
# a temporary list that will be overwritten each time the loop runs
tmp_row = []
# get all cells from row
td_list = row.findAll("td")
# loop over all the td's in the current row - a loop inside a loop
for cell in td_list:
# add one td (cell) to the list
tmp_row.append( cell.get_text() )
# when all td's are in the tmp_row list - write one row to csv
c.writerow(tmp_row)
# close the file
csvfile.close()