計算機科学のブログ

ほしい物リスト

Python - Working with HTML: Web Scraping - gazpacho, Soup, find method, table, rows, data, tr, td

Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly GuidePaul Barry(著)、 O’Reilly Mediaの Chapter 9.(Working with HTML: Web Scraping)、EXERCISE(441/682)の解答を求めてみる。

Jupyter(コード、入出力結果)

webapp/WorldRecords.ipynb

table: gazpacho.soup.Soup
for i, table in enumerate(soup.find('table', mode='all')):
    rows = table.find('tr', mode='all')
    last_row = rows[-1]
    tds = last_row.find('td', mode='all')
    print(f'{i}: {len(rows)}, {len(tds)}')
0: 22, 9
1: 1, 2
2: 21, 9
3: 3, 9
4: 25, 9
5: 26, 9
6: 4, 9
7: 26, 11
8: 1, 2
9: 7, 1
10: 5, 1
11: 8, 1