Add VT2-examples: all 6 chapters of 'Visualize This' (110 files) — data scraping, ETL, trends, categorical, multivariate, spatial, distribution analysis
This commit is contained in:
1 parent
088771a9cf
commit
b42b866a8c
110 files changed
+400895
No files matched your search
@@ -0,0 +1,34 @@
|
||||
from bs4 import BeautifulSoup
|
||||
import urllib.request
|
||||
|
||||
base_url = 'http://book.flowingdata.com/vt2/ch3/scraping/'
|
||||
|
||||
# Header
|
||||
print("famindex,AGE,FTOTINC")
|
||||
|
||||
for i in range(1, 21):
|
||||
|
||||
# Page URL, based on page number
|
||||
page_url = base_url + 'family-income-page'+str(i)+'.html'
|
||||
|
||||
# Open the page
|
||||
req = urllib.request.Request(page_url)
|
||||
with urllib.request.urlopen(req) as response:
|
||||
|
||||
# Save page contents.
|
||||
the_page = response.read()
|
||||
|
||||
# Parse.
|
||||
soup = BeautifulSoup(the_page, features="html.parser")
|
||||
|
||||
# Get the table rows
|
||||
rows = soup.find_all("tr")
|
||||
|
||||
# Save each row of data
|
||||
for j in range(1, len(rows)):
|
||||
|
||||
# Get the cells in a row
|
||||
cells = rows[j].findAll("td")
|
||||
|
||||
# Comma-delimited row
|
||||
print(cells[0].string + "," + cells[1].string + "," + cells[2].string)
|
||||
Reference in new issue
Block a user