To parse a table on a Wikipedia page in Python, you can use the BeautifulSoup library to extract the HTML code of the page and then locate the table element using its CSS class or other attributes. Here is an example of how to extract a table from a Wikipedia page:
main.py829 chars25 lines
In this example, we extract the table from the wikipedia page that contains a list of countries by population, and then loop through its rows to extract the data from each cell. We use the find()
method of BeautifulSoup to locate the table element by its CSS class wikitable sortable
. Finally, we extract the country name and population from the second and third cells of each row, respectively, and print them out.
gistlibby LogSnag