-
-
Save fogonwater/e7039f8e34e3c8c7487b to your computer and use it in GitHub Desktop.
minimal wikipedia beautiful soup table parser from local file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
from pprint import pprint as pp | |
report = [] | |
# open local version | |
soup = BeautifulSoup(open('airports.html'), 'html.parser') | |
# assumes one matching table on page with class wikitable | |
table = soup.find('table', {'class' : 'wikitable'}) | |
for row in table.findAll('tr'): | |
tds = row.find_all('td') | |
items = [td.text.strip() for td in tds] | |
# ignore th element rows | |
if items: | |
report.append(items) | |
pp(report) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment