I was interested in the tables in MediaWiki Version display such as https://en.wikipedia.org/wiki/Special:Version
unit test
from unittest import TestCase import pprint class TestHtmlTables(TestCase): ''' test the HTML Tables parsere ''' def testHtmlTables(self): url="https://en.wikipedia.org/wiki/Special:Version" html_table=HtmlTable(url) tables=html_table.get_tables("h2") pp = pprint.PrettyPrinter(indent=2) debug=True if debug: pp.pprint(tables) pass
HtmlTable.py
''' Created on 2022-10-25 @author: wf ''' from bs4 import BeautifulSoup from urllib.request import Request, urlopen class HtmlTable(object): ''' HtmlTable ''' def __init__(self, url): ''' Constructor ''' req = Request(url, headers={'User-Agent': 'Mozilla/5.0'}) self.html_page = urlopen(req).read() self.soup = BeautifulSoup(self.html_page, 'html.parser') def get_tables(self,header_tag:str=None)->dict: """ get all tables from my soup as a list of list of dicts Args: header_tag(str): if set search the table name from the given header tag Return: dict: the list of list of dicts for all tables """ tables = {} for i,table in enumerate(self.soup.find_all("table")): fields = [] table_data=[] for tr in table.find_all('tr', recursive=True): for th in tr.find_all('th', recursive=True): fields.append(th.text) for tr in table.find_all('tr', recursive=True): record= {} for i, td in enumerate(tr.find_all('td', recursive=True)): record[fields[i]] = td.text if record: table_data.append(record) if header_tag is not None: header=table.find_previous_sibling(header_tag) table_name=header.text else: table_name=f"table{i}" tables[table_name]=(table_data) return tables
Result
Finding files... done. Importing test modules ... done. Tests to run: ['TestHtmlTables.testHtmlTables'] testHtmlTables (tests.test_html_table.TestHtmlTables) ... Starting test testHtmlTables, debug=False ... { 'Entry point URLs': [ {'Entry point': 'Article path', 'URL': '/wiki/$1'}, {'Entry point': 'Script path', 'URL': '/w'}, {'Entry point': 'index.php', 'URL': '/w/index.php'}, {'Entry point': 'api.php', 'URL': '/w/api.php'}, {'Entry point': 'rest.php', 'URL': '/w/rest.php'}], 'Installed extensions': [ { 'Description': 'Brad Jorsch', 'Extension': '1.0 (b9a7bff) 01:45, 9 October ' '2022', 'License': 'Get a summary of logged API feature ' 'usages for a user agent', 'Special pages': 'ApiFeatureUsage', 'Version': 'GPL-2.0-or-later'}, { 'Description': 'Brion Vibber, Kunal Mehta, Sam ' 'Reed, Aaron Schulz, Brad Jorsch, ' 'Umherirrender, Marius Hoch, ' 'Andrew Garrett, Chris Steipp, ' 'Tim Starling, Gergő Tisza, ' 'Alexandre Emsenhuber, Victor ' 'Vasiliev, Glaisher, DannyS712, ' 'Peter Gehres, Bryan Davis, James ' 'D. Forrester, Taavi Väänänen and ' 'Alexander Vorwerk', 'Extension': '– (df2982e) 23:10, 13 October 2022', 'License': 'Merge account across wikis of the ' 'Wikimedia Foundation', 'Special pages': 'CentralAuth', 'Version': 'GPL-2.0-or-later'}, { 'Description': 'Tim Starling and Aaron Schulz', 'Extension': '2.5 (648cfe0) 06:20, 17 October ' '2022', 'License': 'Grants users with the appropriate ' 'permission the ability to check ' "users' IP addresses and other " 'information', 'Special pages': 'CheckUser', 'Version': 'GPL-2.0-or-later'}, { 'Description': 'Ævar Arnfjörð Bjarmason and ' 'James D. Forrester', 'Extension': '– (2cf4aaa) 06:41, 14 October 2022', 'License': 'Adds a citation special page and ' 'toolbox link', 'Special pages': 'CiteThisPage', 'Version': 'GPL-2.0-or-later'}, { 'Description': 'PediaPress GmbH, Siebrand ' 'Mazeland and Marcin Cieślak', 'Extension': '1.8.0 (324e738) 06:20, 17 October ' '2022', 'License': 'Create books', 'Special pages': 'Collection', 'Version': 'GPL-2.0-or-later'}, { 'Description': 'Amir Aharoni, David Chan, Joel ' 'Sahleen, Kartik Mistry, Niklas ' 'Laxström, Pau Giner, Petar ' 'Petković, Runa Bhattacharjee, ' 'Santhosh Thottingal, Siebrand ' 'Mazeland, Sucheta Ghoshal and ' 'others', 'Extension': '– (56fe095) 11:56, 17 October 2022', 'License': 'Makes it easy to translate content ' 'pages', 'Special pages': 'ContentTranslation', 'Version': 'GPL-2.0-or-later'}, { 'Description': 'Andrew Garrett, Ryan Kaldari, ' 'Benny Situ, Luke Welling, Kunal ' 'Mehta, Moriel Schottlender, Jon ' 'Robson and Roan Kattouw', 'Extension': '– (cd01f9b) 06:21, 17 October 2022', 'License': 'System for notifying users about ' 'events and messages', 'Special pages': 'Echo', 'Version': 'MIT'}, .. 'Installed libraries': [ { 'Authors': 'Benjamin Eberlei and Richard Quadling', 'Description': 'Thin assertion library for input ' 'validation in business models.', 'Library': 'beberlei/assert', 'License': 'BSD-2-Clause', 'Version': '3.3.2'}, { 'Authors': '', 'Description': 'Arbitrary-precision arithmetic ' 'library', 'Library': 'brick/math', 'License': 'MIT', 'Version': '0.8.17'}, { 'Authors': 'Christian Riesen', 'Description': 'Base32 encoder/decoder according ' 'to RFC 4648', 'Library': 'christian-riesen/base32', 'License': 'MIT', 'Version': '1.6.0'}, ... { 'Authors': 'Readers Web Team, Trevor Parscal, Roan ' 'Kattouw, Alex Hollender, Bernard Wang, ' 'Clare Ming, Jan Drewniak, Jon Robson, ' 'Nick Ray, Sam Smith, Stephen Niedzielski ' 'and Volker E.', 'Description': 'Provides 2 Vector skins:\n' '\n' '2011 - The Modern version of MonoBook ' 'with fresh look and many usability ' 'improvements.\n' '2022 - The Vector built as part of ' 'the WMF mw:Desktop Improvements ' 'project.', 'License': 'GPL-2.0-or-later', 'Skin': 'Vector', 'Version': '1.0.0 (93f11b3) 20:24, 17 October 2022'}], 'Installed software': [ { 'Product': 'MediaWiki', 'Version': '1.40.0-wmf.6 (bb4c5db)17:39, 17 ' 'October 2022'}, {'Product': 'PHP', 'Version': '7.4.30 (fpm-fcgi)'}, { 'Product': 'MariaDB', 'Version': '10.4.25-MariaDB-log'}, {'Product': 'ICU', 'Version': '63.1'}, {'Product': 'Pygments', 'Version': '2.10.0'}, {'Product': 'LilyPond', 'Version': '2.22.0'}, {'Product': 'Elasticsearch', 'Version': '7.10.2'}, {'Product': 'LuaSandbox', 'Version': '4.0.2'}, {'Product': 'Lua', 'Version': '5.1.5'}]} test testHtmlTables, debug=False took 1.2 s ok ---------------------------------------------------------------------- Ran 1 test in 1.204s OK