17.
Extracting Tabular Data from HTML

Dr. W.J.B. Mattingly
Smithsonian Data Science Lab and United States Holocaust Memorial Museum
March 2022

17.1. Covered in this Chapter

  1. Table Tags

  2. TR Tags

  3. TH and TD Tags

  4. How to parse an HTML Table

17.2. Introduction

In this chapter, we will put our skills to work! We will learn how to extract tabular data via requests and BeautifulSoup. We will work with a lot of the commands and methods we saw in the last chapter, but we will not be trying to extract p tags, rather tabular data from the same Wikipedia page. All of this will allow you to apply your skills to the final challenge of this textbook (introduced in the next chapter).

First, let’s import the same libraries as in the last chapter, requests and BeautifulSoup.

import requests
from bs4 import BeautifulSoup

Let’s also go ahead and make the same string object, the url of the page we want to scrape.

url = "https://en.wikipedia.org/wiki/List_of_French_monarchs"

Now, let’s dive in!

17.3. Finding the Tables on the Page

In the cell below, we will make the call to the Wikipedia server and convert the HTML content into a soup object as we saw in the last chapter.

s =  requests.get(url)
soup = BeautifulSoup(s.content)

Now that we have our soup object, we can start to parse it. Tables are often structured in HTML the same way across all sites. The main tag used is the table tag. On Wikipedia, there are multiple kinds of tables. The class we want is a table class called “wikitable”. Let’s go ahead and grab all these tables and print off how many we have on the page.

tables = soup.find_all("table", {"class": "wikitable"})
print (len(tables))
11

17.4. Grabbing Rows of a Table

Excellent! Now that hwe have the tables, let’s take a look at the first one’s HTML.

first_table = tables[0]
print (first_table)
<table class="wikitable" style="text-align:center;" width="97%">
<tbody><tr>
<th width="10%">Portrait
</th>
<th width="16%">Name
</th>
<th width="15%">Start
</th>
<th width="15%">End
</th>
<th width="22%">Succession / Notes
</th>
<th width="15%">Title
</th>
<th width="4%">Ref.
</th></tr>
<tr>
<td><a class="image" href="/wiki/File:Charles_II,_Holy_Roman_Emperor.jpg"><img alt="Charles II, Holy Roman Emperor.jpg" data-file-height="1842" data-file-width="1228" decoding="async" height="165" src="//upload.wikimedia.org/wikipedia/commons/thumb/7/77/Charles_II%2C_Holy_Roman_Emperor.jpg/110px-Charles_II%2C_Holy_Roman_Emperor.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/7/77/Charles_II%2C_Holy_Roman_Emperor.jpg/165px-Charles_II%2C_Holy_Roman_Emperor.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/7/77/Charles_II%2C_Holy_Roman_Emperor.jpg/220px-Charles_II%2C_Holy_Roman_Emperor.jpg 2x" width="110"/></a>
</td>
<td><a class="mw-redirect" href="/wiki/Charles_II_the_Bald" title="Charles II the Bald"><b>Charles II</b><br/>  "the Bald"</a><sup class="reference" id="cite_ref-:0_8-0"><a href="#cite_note-:0-8">[b]</a></sup>
</td>
<td><abbr title="circa">c.</abbr> 10 August 843<sup class="reference" id="cite_ref-9"><a href="#cite_note-9">[c]</a></sup><sup class="reference" id="cite_ref-10"><a href="#cite_note-10">[i]</a></sup>
</td>
<td>6 October 877
</td>
<td> • Son of <b><a class="mw-redirect" href="/wiki/Louis_I_the_Pious" title="Louis I the Pious">Louis I</a></b> and grandson of <a href="/wiki/Charlemagne" title="Charlemagne">Charlemagne</a> (<b><a class="mw-redirect" href="/wiki/Charles_I_of_France" title="Charles I of France">Charles I</a></b>)
</td>
<td><i><a class="mw-redirect" href="/wiki/King_of_the_Franks" title="King of the Franks">King of the Franks</a></i><br/>(840–77)<br/><i><a href="/wiki/Holy_Roman_Emperor" title="Holy Roman Emperor">Emperor of the Romans</a></i> (875–77)
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189_11-0"><a href="#cite_note-FOOTNOTEThoison1888189-11">[7]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-0"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-13"><a href="#cite_note-13">[9]</a></sup><br/><sup class="reference" id="cite_ref-14"><a href="#cite_note-14">[10]</a></sup><br/>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg"><img alt="Denier sous Louis II dit le Bègue.jpg" data-file-height="1380" data-file-width="2773" decoding="async" height="55" src="//upload.wikimedia.org/wikipedia/commons/thumb/a/ad/Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg/110px-Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/a/ad/Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg/165px-Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/a/ad/Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg/220px-Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg 2x" width="110"/></a>
</td>
<td><a class="mw-redirect" href="/wiki/Louis_II_the_Stammerer" title="Louis II the Stammerer"><b>Louis II</b><br/> "the Stammerer"</a><sup class="reference" id="cite_ref-15"><a href="#cite_note-15">[d]</a></sup>
</td>
<td>6 October 877<sup class="reference" id="cite_ref-16"><a href="#cite_note-16">[ii]</a></sup>
</td>
<td>10 April 879
</td>
<td> • Son of Charles the Bald
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-1"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189_11-1"><a href="#cite_note-FOOTNOTEThoison1888189-11">[7]</a></sup><br/><sup class="reference" id="cite_ref-c_17-0"><a href="#cite_note-c-17">[11]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Denier_sous_Louis_III.jpg"><img alt="Denier sous Louis III.jpg" data-file-height="1140" data-file-width="2347" decoding="async" height="53" src="//upload.wikimedia.org/wikipedia/commons/thumb/6/66/Denier_sous_Louis_III.jpg/110px-Denier_sous_Louis_III.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/6/66/Denier_sous_Louis_III.jpg/165px-Denier_sous_Louis_III.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/6/66/Denier_sous_Louis_III.jpg/220px-Denier_sous_Louis_III.jpg 2x" width="110"/></a>
</td>
<td><a href="/wiki/Louis_III_of_France" title="Louis III of France"><b>Louis III</b></a>
</td>
<td>10 April 879<sup class="reference" id="cite_ref-18"><a href="#cite_note-18">[iii]</a></sup>
</td>
<td>5 August 882
</td>
<td> • Son of Louis the Stammerer
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-2"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189._The_text_says_&quot;3_August&quot;,_but_the_''[[Annales_Vedastini]]''_clearly_indicate_&quot;5_August&quot;_([httpwwwthelatinlibrarycomannalesvedastinihtml_nonis_augusti])_19-0"><a href="#cite_note-FOOTNOTEThoison1888189._The_text_says_&quot;3_August&quot;,_but_the_''[[Annales_Vedastini]]''_clearly_indicate_&quot;5_August&quot;_([httpwwwthelatinlibrarycomannalesvedastinihtml_nonis_augusti])-19">[12]</a></sup><br/><sup class="reference" id="cite_ref-ca_20-0"><a href="#cite_note-ca-20">[13]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Denier_sous_Carloman_II.jpg"><img alt="Denier sous Carloman II.jpg" data-file-height="1323" data-file-width="2652" decoding="async" height="55" src="//upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Denier_sous_Carloman_II.jpg/110px-Denier_sous_Carloman_II.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Denier_sous_Carloman_II.jpg/165px-Denier_sous_Carloman_II.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Denier_sous_Carloman_II.jpg/220px-Denier_sous_Carloman_II.jpg 2x" width="110"/></a>
</td>
<td><a href="/wiki/Carloman_II" title="Carloman II"><b>Carloman II</b></a>
</td>
<td>10 April 879<sup class="reference" id="cite_ref-21"><a href="#cite_note-21">[iv]</a></sup>
</td>
<td>12 December 884
</td>
<td> • Son of Louis the Stammerer
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-3"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189_11-2"><a href="#cite_note-FOOTNOTEThoison1888189-11">[7]</a></sup><br/><sup class="reference" id="cite_ref-ca_20-1"><a href="#cite_note-ca-20">[13]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Sceau_de_Charles_le_gros.jpg"><img alt="Sceau de Charles le gros.jpg" data-file-height="291" data-file-width="302" decoding="async" height="106" src="//upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Sceau_de_Charles_le_gros.jpg/110px-Sceau_de_Charles_le_gros.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Sceau_de_Charles_le_gros.jpg/165px-Sceau_de_Charles_le_gros.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Sceau_de_Charles_le_gros.jpg/220px-Sceau_de_Charles_le_gros.jpg 2x" width="110"/></a>
</td>
<td><a href="/wiki/Charles_the_Fat" title="Charles the Fat"><b>Charles</b> (<b>III</b>)<br/> "the Fat"</a><sup class="reference" id="cite_ref-22"><a href="#cite_note-22">[e]</a></sup>
</td>
<td>12 December 884<sup class="reference" id="cite_ref-23"><a href="#cite_note-23">[v]</a></sup>
</td>
<td>13 January 888
</td>
<td> • Son of <a class="mw-redirect" href="/wiki/Louis_II_the_German" title="Louis II the German">Louis II the German</a><br/> • Cousin once removed of <a href="/wiki/Carloman_II" title="Carloman II">Carloman II</a> and <a href="/wiki/Louis_III_of_France" title="Louis III of France">Louis III</a><br/> • Grandson of <a class="mw-redirect" href="/wiki/Louis_I_the_Pious" title="Louis I the Pious">Louis I</a>
</td>
<td><i><a class="mw-redirect" href="/wiki/King_of_East_Francia" title="King of East Francia">King of East Francia</a></i><br/>(871–88)<br/><a href="/wiki/Holy_Roman_Emperor" title="Holy Roman Emperor"><i>Emperor of the Romans</i></a> (881–88)
</td>
<td><sup class="reference" id="cite_ref-24"><a href="#cite_note-24">[14]</a></sup><br/><sup class="reference" id="cite_ref-25"><a href="#cite_note-25">[15]</a></sup>
</td></tr>
<tr>
<td bgcolor="#d6ebff"><a class="image" href="/wiki/File:Denier_de_Blois_sous_Eudes_de_France.jpg"><img alt="Denier de Blois sous Eudes de France.jpg" data-file-height="1865" data-file-width="3739" decoding="async" height="55" src="//upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Denier_de_Blois_sous_Eudes_de_France.jpg/110px-Denier_de_Blois_sous_Eudes_de_France.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Denier_de_Blois_sous_Eudes_de_France.jpg/165px-Denier_de_Blois_sous_Eudes_de_France.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Denier_de_Blois_sous_Eudes_de_France.jpg/220px-Denier_de_Blois_sous_Eudes_de_France.jpg 2x" width="110"/></a>
</td>
<td bgcolor="#d6ebff"><b><a class="mw-redirect" href="/wiki/Eudes_of_France" title="Eudes of France">Eudes</a></b><br/><small>(<a href="/wiki/Robertians" title="Robertians">Robertians</a>)</small>
</td>
<td bgcolor="#d6ebff">13 January 888<sup class="reference" id="cite_ref-26"><a href="#cite_note-26">[vi]</a></sup>
</td>
<td bgcolor="#d6ebff">1 January 898
</td>
<td bgcolor="#d6ebff"> • Son of <a href="/wiki/Robert_the_Strong" title="Robert the Strong">Robert the Strong</a><br/> • Rivaled by Charles III.
</td>
<td bgcolor="#d6ebff"><i>King of the Franks</i>
</td>
<td bgcolor="#d6ebff"><sup class="reference" id="cite_ref-27"><a href="#cite_note-27">[16]</a></sup><br/><sup class="reference" id="cite_ref-c_17-1"><a href="#cite_note-c-17">[11]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Denier_sous_Charles_III_le_Simple.jpg"><img alt="Denier sous Charles III le Simple.jpg" data-file-height="1845" data-file-width="3699" decoding="async" height="55" src="//upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Denier_sous_Charles_III_le_Simple.jpg/110px-Denier_sous_Charles_III_le_Simple.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Denier_sous_Charles_III_le_Simple.jpg/165px-Denier_sous_Charles_III_le_Simple.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Denier_sous_Charles_III_le_Simple.jpg/220px-Denier_sous_Charles_III_le_Simple.jpg 2x" width="110"/></a>
</td>
<td><a href="/wiki/Charles_the_Simple" title="Charles the Simple"><b>Charles III</b><br/>"the Simple"</a>
</td>
<td>1 January 898<sup class="reference" id="cite_ref-28"><a href="#cite_note-28">[vii]</a></sup>
</td>
<td>29 June 922
</td>
<td> • Posthumous son of Louis II<br/> • Deposed
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-4"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189_11-3"><a href="#cite_note-FOOTNOTEThoison1888189-11">[7]</a></sup>
</td></tr>
<tr>
<td bgcolor="#d6ebff"><a class="image" href="/wiki/File:Robert_Ier_roi_des_Francs.jpg"><img alt="Robert Ier roi des Francs.jpg" data-file-height="266" data-file-width="269" decoding="async" height="89" src="//upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Robert_Ier_roi_des_Francs.jpg/90px-Robert_Ier_roi_des_Francs.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Robert_Ier_roi_des_Francs.jpg/135px-Robert_Ier_roi_des_Francs.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Robert_Ier_roi_des_Francs.jpg/180px-Robert_Ier_roi_des_Francs.jpg 2x" width="90"/></a>
</td>
<td bgcolor="#d6ebff"><b><a href="/wiki/Robert_I_of_France" title="Robert I of France">Robert I</a></b><br/><small>(<a href="/wiki/Robertians" title="Robertians">Robertians</a>)</small>
</td>
<td bgcolor="#d6ebff">29 June 922<sup class="reference" id="cite_ref-29"><a href="#cite_note-29">[viii]</a></sup>
</td>
<td bgcolor="#d6ebff">15 June 923
</td>
<td bgcolor="#d6ebff"> • Son of <a href="/wiki/Robert_the_Strong" title="Robert the Strong">Robert the Strong</a> and younger brother of Odo<br/> •  Killed at the <a href="/wiki/Battle_of_Soissons_(923)" title="Battle of Soissons (923)">Battle of Soissons</a>
</td>
<td bgcolor="#d6ebff"><i>King of the Franks</i>
</td>
<td bgcolor="#d6ebff"><sup class="reference" id="cite_ref-30"><a href="#cite_note-30">[17]</a></sup><br/><sup class="reference" id="cite_ref-c_17-2"><a href="#cite_note-c-17">[11]</a></sup>
</td></tr>
<tr>
<td bgcolor="#d6ebff"><a class="image" href="/wiki/File:Denier_sous_Raoul_de_France.jpg"><img alt="Denier sous Raoul de France.jpg" data-file-height="1922" data-file-width="3901" decoding="async" height="54" src="//upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Denier_sous_Raoul_de_France.jpg/110px-Denier_sous_Raoul_de_France.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Denier_sous_Raoul_de_France.jpg/165px-Denier_sous_Raoul_de_France.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Denier_sous_Raoul_de_France.jpg/220px-Denier_sous_Raoul_de_France.jpg 2x" width="110"/></a>
</td>
<td bgcolor="#d6ebff"><a href="/wiki/Rudolph_of_France" title="Rudolph of France"><b>Rudolph</b></a><br/><small>(<a href="/wiki/Bosonids" title="Bosonids">Bosonids</a>)</small>
</td>
<td bgcolor="#d6ebff">15 June 923<sup class="reference" id="cite_ref-31"><a href="#cite_note-31">[ix]</a></sup>
</td>
<td bgcolor="#d6ebff">14 January 936
</td>
<td bgcolor="#d6ebff"> • Son of <a href="/wiki/Richard,_Duke_of_Burgundy" title="Richard, Duke of Burgundy">Richard, Duke of Burgundy</a><br/> • Son-in-law of Robert I
</td>
<td bgcolor="#d6ebff"><i>King of the Franks</i>
</td>
<td bgcolor="#d6ebff"><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-5"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189_11-4"><a href="#cite_note-FOOTNOTEThoison1888189-11">[7]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Denier_Chinon_954,_obverse.jpg"><img alt="Denier Chinon 954, obverse.jpg" data-file-height="231" data-file-width="252" decoding="async" height="92" src="//upload.wikimedia.org/wikipedia/en/thumb/e/e3/Denier_Chinon_954%2C_obverse.jpg/100px-Denier_Chinon_954%2C_obverse.jpg" srcset="//upload.wikimedia.org/wikipedia/en/thumb/e/e3/Denier_Chinon_954%2C_obverse.jpg/150px-Denier_Chinon_954%2C_obverse.jpg 1.5x, //upload.wikimedia.org/wikipedia/en/thumb/e/e3/Denier_Chinon_954%2C_obverse.jpg/200px-Denier_Chinon_954%2C_obverse.jpg 2x" width="100"/></a>
</td>
<td><a href="/wiki/Louis_IV_of_France" title="Louis IV of France"><b>Louis IV</b><br/>"d'Outremer"</a>
</td>
<td>19 June 936<sup class="reference" id="cite_ref-32"><a href="#cite_note-32">[x]</a></sup>
</td>
<td>10 September 954
</td>
<td> • Son of Charles III the Simple
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-6"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189_11-5"><a href="#cite_note-FOOTNOTEThoison1888189-11">[7]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:LotharRichardII10thcentury.JPG"><img alt="LotharRichardII10thcentury.JPG" data-file-height="360" data-file-width="268" decoding="async" height="134" src="//upload.wikimedia.org/wikipedia/commons/thumb/e/e5/LotharRichardII10thcentury.JPG/100px-LotharRichardII10thcentury.JPG" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/e/e5/LotharRichardII10thcentury.JPG/150px-LotharRichardII10thcentury.JPG 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/e/e5/LotharRichardII10thcentury.JPG/199px-LotharRichardII10thcentury.JPG 2x" width="100"/></a>
</td>
<td><a href="/wiki/Lothair_of_France" title="Lothair of France"><b>Lothair</b></a><sup class="reference" id="cite_ref-33"><a href="#cite_note-33">[f]</a></sup>
</td>
<td>10 September 954<sup class="reference" id="cite_ref-34"><a href="#cite_note-34">[xi]</a></sup>
</td>
<td>2 March 986
</td>
<td> • Son of Louis IV
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-7"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888190_35-0"><a href="#cite_note-FOOTNOTEThoison1888190-35">[18]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Louis_V_of_France.jpg"><img alt="Louis V of France.jpg" data-file-height="313" data-file-width="302" decoding="async" height="93" src="//upload.wikimedia.org/wikipedia/commons/thumb/5/58/Louis_V_of_France.jpg/90px-Louis_V_of_France.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/5/58/Louis_V_of_France.jpg/135px-Louis_V_of_France.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/5/58/Louis_V_of_France.jpg/179px-Louis_V_of_France.jpg 2x" width="90"/></a>
</td>
<td><a href="/wiki/Louis_V_of_France" title="Louis V of France"><b>Louis V</b><br/>"the Do-Nothing"</a>
</td>
<td>2 March 986<sup class="reference" id="cite_ref-36"><a href="#cite_note-36">[xii]</a></sup>
</td>
<td>22 May 987
</td>
<td> • Son of Lothair
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-37"><a href="#cite_note-37">[19]</a></sup><br/><sup class="reference" id="cite_ref-38"><a href="#cite_note-38">[20]</a></sup>
</td></tr></tbody></table>

A table is a combination of 2 things: rows and cells. Rows will almost always be “tr” tags. This stands for table row. Cells with either be th tags or td tags. The th tag stands for table header. This will usually be used in the first row that indicates the name of the column. The td tag stands for Table Data cell. These are the cells that start on the first row that contains data and continue on down until the table ends. Because tables are precise structurally, there will always be the same name of headers as there are columns of data. We can use this structure to our advantage.

Let’s find all the rows in the first table

rows = first_table.find_all("tr")

Excellent! Now, let’s iterate over all the rows.

for row in rows:
    print (row)
<tr>
<th width="10%">Portrait
</th>
<th width="16%">Name
</th>
<th width="15%">Start
</th>
<th width="15%">End
</th>
<th width="22%">Succession / Notes
</th>
<th width="15%">Title
</th>
<th width="4%">Ref.
</th></tr>
<tr>
<td><a class="image" href="/wiki/File:Charles_II,_Holy_Roman_Emperor.jpg"><img alt="Charles II, Holy Roman Emperor.jpg" data-file-height="1842" data-file-width="1228" decoding="async" height="165" src="//upload.wikimedia.org/wikipedia/commons/thumb/7/77/Charles_II%2C_Holy_Roman_Emperor.jpg/110px-Charles_II%2C_Holy_Roman_Emperor.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/7/77/Charles_II%2C_Holy_Roman_Emperor.jpg/165px-Charles_II%2C_Holy_Roman_Emperor.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/7/77/Charles_II%2C_Holy_Roman_Emperor.jpg/220px-Charles_II%2C_Holy_Roman_Emperor.jpg 2x" width="110"/></a>
</td>
<td><a class="mw-redirect" href="/wiki/Charles_II_the_Bald" title="Charles II the Bald"><b>Charles II</b><br/>  "the Bald"</a><sup class="reference" id="cite_ref-:0_8-0"><a href="#cite_note-:0-8">[b]</a></sup>
</td>
<td><abbr title="circa">c.</abbr> 10 August 843<sup class="reference" id="cite_ref-9"><a href="#cite_note-9">[c]</a></sup><sup class="reference" id="cite_ref-10"><a href="#cite_note-10">[i]</a></sup>
</td>
<td>6 October 877
</td>
<td> • Son of <b><a class="mw-redirect" href="/wiki/Louis_I_the_Pious" title="Louis I the Pious">Louis I</a></b> and grandson of <a href="/wiki/Charlemagne" title="Charlemagne">Charlemagne</a> (<b><a class="mw-redirect" href="/wiki/Charles_I_of_France" title="Charles I of France">Charles I</a></b>)
</td>
<td><i><a class="mw-redirect" href="/wiki/King_of_the_Franks" title="King of the Franks">King of the Franks</a></i><br/>(840–77)<br/><i><a href="/wiki/Holy_Roman_Emperor" title="Holy Roman Emperor">Emperor of the Romans</a></i> (875–77)
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189_11-0"><a href="#cite_note-FOOTNOTEThoison1888189-11">[7]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-0"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-13"><a href="#cite_note-13">[9]</a></sup><br/><sup class="reference" id="cite_ref-14"><a href="#cite_note-14">[10]</a></sup><br/>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg"><img alt="Denier sous Louis II dit le Bègue.jpg" data-file-height="1380" data-file-width="2773" decoding="async" height="55" src="//upload.wikimedia.org/wikipedia/commons/thumb/a/ad/Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg/110px-Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/a/ad/Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg/165px-Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/a/ad/Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg/220px-Denier_sous_Louis_II_dit_le_B%C3%A8gue.jpg 2x" width="110"/></a>
</td>
<td><a class="mw-redirect" href="/wiki/Louis_II_the_Stammerer" title="Louis II the Stammerer"><b>Louis II</b><br/> "the Stammerer"</a><sup class="reference" id="cite_ref-15"><a href="#cite_note-15">[d]</a></sup>
</td>
<td>6 October 877<sup class="reference" id="cite_ref-16"><a href="#cite_note-16">[ii]</a></sup>
</td>
<td>10 April 879
</td>
<td> • Son of Charles the Bald
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-1"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189_11-1"><a href="#cite_note-FOOTNOTEThoison1888189-11">[7]</a></sup><br/><sup class="reference" id="cite_ref-c_17-0"><a href="#cite_note-c-17">[11]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Denier_sous_Louis_III.jpg"><img alt="Denier sous Louis III.jpg" data-file-height="1140" data-file-width="2347" decoding="async" height="53" src="//upload.wikimedia.org/wikipedia/commons/thumb/6/66/Denier_sous_Louis_III.jpg/110px-Denier_sous_Louis_III.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/6/66/Denier_sous_Louis_III.jpg/165px-Denier_sous_Louis_III.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/6/66/Denier_sous_Louis_III.jpg/220px-Denier_sous_Louis_III.jpg 2x" width="110"/></a>
</td>
<td><a href="/wiki/Louis_III_of_France" title="Louis III of France"><b>Louis III</b></a>
</td>
<td>10 April 879<sup class="reference" id="cite_ref-18"><a href="#cite_note-18">[iii]</a></sup>
</td>
<td>5 August 882
</td>
<td> • Son of Louis the Stammerer
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-2"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189._The_text_says_&quot;3_August&quot;,_but_the_''[[Annales_Vedastini]]''_clearly_indicate_&quot;5_August&quot;_([httpwwwthelatinlibrarycomannalesvedastinihtml_nonis_augusti])_19-0"><a href="#cite_note-FOOTNOTEThoison1888189._The_text_says_&quot;3_August&quot;,_but_the_''[[Annales_Vedastini]]''_clearly_indicate_&quot;5_August&quot;_([httpwwwthelatinlibrarycomannalesvedastinihtml_nonis_augusti])-19">[12]</a></sup><br/><sup class="reference" id="cite_ref-ca_20-0"><a href="#cite_note-ca-20">[13]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Denier_sous_Carloman_II.jpg"><img alt="Denier sous Carloman II.jpg" data-file-height="1323" data-file-width="2652" decoding="async" height="55" src="//upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Denier_sous_Carloman_II.jpg/110px-Denier_sous_Carloman_II.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Denier_sous_Carloman_II.jpg/165px-Denier_sous_Carloman_II.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Denier_sous_Carloman_II.jpg/220px-Denier_sous_Carloman_II.jpg 2x" width="110"/></a>
</td>
<td><a href="/wiki/Carloman_II" title="Carloman II"><b>Carloman II</b></a>
</td>
<td>10 April 879<sup class="reference" id="cite_ref-21"><a href="#cite_note-21">[iv]</a></sup>
</td>
<td>12 December 884
</td>
<td> • Son of Louis the Stammerer
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-3"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189_11-2"><a href="#cite_note-FOOTNOTEThoison1888189-11">[7]</a></sup><br/><sup class="reference" id="cite_ref-ca_20-1"><a href="#cite_note-ca-20">[13]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Sceau_de_Charles_le_gros.jpg"><img alt="Sceau de Charles le gros.jpg" data-file-height="291" data-file-width="302" decoding="async" height="106" src="//upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Sceau_de_Charles_le_gros.jpg/110px-Sceau_de_Charles_le_gros.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Sceau_de_Charles_le_gros.jpg/165px-Sceau_de_Charles_le_gros.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Sceau_de_Charles_le_gros.jpg/220px-Sceau_de_Charles_le_gros.jpg 2x" width="110"/></a>
</td>
<td><a href="/wiki/Charles_the_Fat" title="Charles the Fat"><b>Charles</b> (<b>III</b>)<br/> "the Fat"</a><sup class="reference" id="cite_ref-22"><a href="#cite_note-22">[e]</a></sup>
</td>
<td>12 December 884<sup class="reference" id="cite_ref-23"><a href="#cite_note-23">[v]</a></sup>
</td>
<td>13 January 888
</td>
<td> • Son of <a class="mw-redirect" href="/wiki/Louis_II_the_German" title="Louis II the German">Louis II the German</a><br/> • Cousin once removed of <a href="/wiki/Carloman_II" title="Carloman II">Carloman II</a> and <a href="/wiki/Louis_III_of_France" title="Louis III of France">Louis III</a><br/> • Grandson of <a class="mw-redirect" href="/wiki/Louis_I_the_Pious" title="Louis I the Pious">Louis I</a>
</td>
<td><i><a class="mw-redirect" href="/wiki/King_of_East_Francia" title="King of East Francia">King of East Francia</a></i><br/>(871–88)<br/><a href="/wiki/Holy_Roman_Emperor" title="Holy Roman Emperor"><i>Emperor of the Romans</i></a> (881–88)
</td>
<td><sup class="reference" id="cite_ref-24"><a href="#cite_note-24">[14]</a></sup><br/><sup class="reference" id="cite_ref-25"><a href="#cite_note-25">[15]</a></sup>
</td></tr>
<tr>
<td bgcolor="#d6ebff"><a class="image" href="/wiki/File:Denier_de_Blois_sous_Eudes_de_France.jpg"><img alt="Denier de Blois sous Eudes de France.jpg" data-file-height="1865" data-file-width="3739" decoding="async" height="55" src="//upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Denier_de_Blois_sous_Eudes_de_France.jpg/110px-Denier_de_Blois_sous_Eudes_de_France.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Denier_de_Blois_sous_Eudes_de_France.jpg/165px-Denier_de_Blois_sous_Eudes_de_France.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Denier_de_Blois_sous_Eudes_de_France.jpg/220px-Denier_de_Blois_sous_Eudes_de_France.jpg 2x" width="110"/></a>
</td>
<td bgcolor="#d6ebff"><b><a class="mw-redirect" href="/wiki/Eudes_of_France" title="Eudes of France">Eudes</a></b><br/><small>(<a href="/wiki/Robertians" title="Robertians">Robertians</a>)</small>
</td>
<td bgcolor="#d6ebff">13 January 888<sup class="reference" id="cite_ref-26"><a href="#cite_note-26">[vi]</a></sup>
</td>
<td bgcolor="#d6ebff">1 January 898
</td>
<td bgcolor="#d6ebff"> • Son of <a href="/wiki/Robert_the_Strong" title="Robert the Strong">Robert the Strong</a><br/> • Rivaled by Charles III.
</td>
<td bgcolor="#d6ebff"><i>King of the Franks</i>
</td>
<td bgcolor="#d6ebff"><sup class="reference" id="cite_ref-27"><a href="#cite_note-27">[16]</a></sup><br/><sup class="reference" id="cite_ref-c_17-1"><a href="#cite_note-c-17">[11]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Denier_sous_Charles_III_le_Simple.jpg"><img alt="Denier sous Charles III le Simple.jpg" data-file-height="1845" data-file-width="3699" decoding="async" height="55" src="//upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Denier_sous_Charles_III_le_Simple.jpg/110px-Denier_sous_Charles_III_le_Simple.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Denier_sous_Charles_III_le_Simple.jpg/165px-Denier_sous_Charles_III_le_Simple.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Denier_sous_Charles_III_le_Simple.jpg/220px-Denier_sous_Charles_III_le_Simple.jpg 2x" width="110"/></a>
</td>
<td><a href="/wiki/Charles_the_Simple" title="Charles the Simple"><b>Charles III</b><br/>"the Simple"</a>
</td>
<td>1 January 898<sup class="reference" id="cite_ref-28"><a href="#cite_note-28">[vii]</a></sup>
</td>
<td>29 June 922
</td>
<td> • Posthumous son of Louis II<br/> • Deposed
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-4"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189_11-3"><a href="#cite_note-FOOTNOTEThoison1888189-11">[7]</a></sup>
</td></tr>
<tr>
<td bgcolor="#d6ebff"><a class="image" href="/wiki/File:Robert_Ier_roi_des_Francs.jpg"><img alt="Robert Ier roi des Francs.jpg" data-file-height="266" data-file-width="269" decoding="async" height="89" src="//upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Robert_Ier_roi_des_Francs.jpg/90px-Robert_Ier_roi_des_Francs.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Robert_Ier_roi_des_Francs.jpg/135px-Robert_Ier_roi_des_Francs.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/5/5a/Robert_Ier_roi_des_Francs.jpg/180px-Robert_Ier_roi_des_Francs.jpg 2x" width="90"/></a>
</td>
<td bgcolor="#d6ebff"><b><a href="/wiki/Robert_I_of_France" title="Robert I of France">Robert I</a></b><br/><small>(<a href="/wiki/Robertians" title="Robertians">Robertians</a>)</small>
</td>
<td bgcolor="#d6ebff">29 June 922<sup class="reference" id="cite_ref-29"><a href="#cite_note-29">[viii]</a></sup>
</td>
<td bgcolor="#d6ebff">15 June 923
</td>
<td bgcolor="#d6ebff"> • Son of <a href="/wiki/Robert_the_Strong" title="Robert the Strong">Robert the Strong</a> and younger brother of Odo<br/> •  Killed at the <a href="/wiki/Battle_of_Soissons_(923)" title="Battle of Soissons (923)">Battle of Soissons</a>
</td>
<td bgcolor="#d6ebff"><i>King of the Franks</i>
</td>
<td bgcolor="#d6ebff"><sup class="reference" id="cite_ref-30"><a href="#cite_note-30">[17]</a></sup><br/><sup class="reference" id="cite_ref-c_17-2"><a href="#cite_note-c-17">[11]</a></sup>
</td></tr>
<tr>
<td bgcolor="#d6ebff"><a class="image" href="/wiki/File:Denier_sous_Raoul_de_France.jpg"><img alt="Denier sous Raoul de France.jpg" data-file-height="1922" data-file-width="3901" decoding="async" height="54" src="//upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Denier_sous_Raoul_de_France.jpg/110px-Denier_sous_Raoul_de_France.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Denier_sous_Raoul_de_France.jpg/165px-Denier_sous_Raoul_de_France.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Denier_sous_Raoul_de_France.jpg/220px-Denier_sous_Raoul_de_France.jpg 2x" width="110"/></a>
</td>
<td bgcolor="#d6ebff"><a href="/wiki/Rudolph_of_France" title="Rudolph of France"><b>Rudolph</b></a><br/><small>(<a href="/wiki/Bosonids" title="Bosonids">Bosonids</a>)</small>
</td>
<td bgcolor="#d6ebff">15 June 923<sup class="reference" id="cite_ref-31"><a href="#cite_note-31">[ix]</a></sup>
</td>
<td bgcolor="#d6ebff">14 January 936
</td>
<td bgcolor="#d6ebff"> • Son of <a href="/wiki/Richard,_Duke_of_Burgundy" title="Richard, Duke of Burgundy">Richard, Duke of Burgundy</a><br/> • Son-in-law of Robert I
</td>
<td bgcolor="#d6ebff"><i>King of the Franks</i>
</td>
<td bgcolor="#d6ebff"><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-5"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189_11-4"><a href="#cite_note-FOOTNOTEThoison1888189-11">[7]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Denier_Chinon_954,_obverse.jpg"><img alt="Denier Chinon 954, obverse.jpg" data-file-height="231" data-file-width="252" decoding="async" height="92" src="//upload.wikimedia.org/wikipedia/en/thumb/e/e3/Denier_Chinon_954%2C_obverse.jpg/100px-Denier_Chinon_954%2C_obverse.jpg" srcset="//upload.wikimedia.org/wikipedia/en/thumb/e/e3/Denier_Chinon_954%2C_obverse.jpg/150px-Denier_Chinon_954%2C_obverse.jpg 1.5x, //upload.wikimedia.org/wikipedia/en/thumb/e/e3/Denier_Chinon_954%2C_obverse.jpg/200px-Denier_Chinon_954%2C_obverse.jpg 2x" width="100"/></a>
</td>
<td><a href="/wiki/Louis_IV_of_France" title="Louis IV of France"><b>Louis IV</b><br/>"d'Outremer"</a>
</td>
<td>19 June 936<sup class="reference" id="cite_ref-32"><a href="#cite_note-32">[x]</a></sup>
</td>
<td>10 September 954
</td>
<td> • Son of Charles III the Simple
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-6"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888189_11-5"><a href="#cite_note-FOOTNOTEThoison1888189-11">[7]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:LotharRichardII10thcentury.JPG"><img alt="LotharRichardII10thcentury.JPG" data-file-height="360" data-file-width="268" decoding="async" height="134" src="//upload.wikimedia.org/wikipedia/commons/thumb/e/e5/LotharRichardII10thcentury.JPG/100px-LotharRichardII10thcentury.JPG" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/e/e5/LotharRichardII10thcentury.JPG/150px-LotharRichardII10thcentury.JPG 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/e/e5/LotharRichardII10thcentury.JPG/199px-LotharRichardII10thcentury.JPG 2x" width="100"/></a>
</td>
<td><a href="/wiki/Lothair_of_France" title="Lothair of France"><b>Lothair</b></a><sup class="reference" id="cite_ref-33"><a href="#cite_note-33">[f]</a></sup>
</td>
<td>10 September 954<sup class="reference" id="cite_ref-34"><a href="#cite_note-34">[xi]</a></sup>
</td>
<td>2 March 986
</td>
<td> • Son of Louis IV
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-FOOTNOTEBrougham1861395_12-7"><a href="#cite_note-FOOTNOTEBrougham1861395-12">[8]</a></sup><br/><sup class="reference" id="cite_ref-FOOTNOTEThoison1888190_35-0"><a href="#cite_note-FOOTNOTEThoison1888190-35">[18]</a></sup>
</td></tr>
<tr>
<td><a class="image" href="/wiki/File:Louis_V_of_France.jpg"><img alt="Louis V of France.jpg" data-file-height="313" data-file-width="302" decoding="async" height="93" src="//upload.wikimedia.org/wikipedia/commons/thumb/5/58/Louis_V_of_France.jpg/90px-Louis_V_of_France.jpg" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/5/58/Louis_V_of_France.jpg/135px-Louis_V_of_France.jpg 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/5/58/Louis_V_of_France.jpg/179px-Louis_V_of_France.jpg 2x" width="90"/></a>
</td>
<td><a href="/wiki/Louis_V_of_France" title="Louis V of France"><b>Louis V</b><br/>"the Do-Nothing"</a>
</td>
<td>2 March 986<sup class="reference" id="cite_ref-36"><a href="#cite_note-36">[xii]</a></sup>
</td>
<td>22 May 987
</td>
<td> • Son of Lothair
</td>
<td><i>King of the Franks</i>
</td>
<td><sup class="reference" id="cite_ref-37"><a href="#cite_note-37">[19]</a></sup><br/><sup class="reference" id="cite_ref-38"><a href="#cite_note-38">[20]</a></sup>
</td></tr>

We can see the precise same HTML that we saw above. Now that we can access the rows, let’s try and access the first row of data.

17.5. Find Cells

To do this, we will need to find each cell. Remember, on row 1, we are working with th tags because these are headers.

first_row = rows[0]
print (first_row)
<tr>
<th width="10%">Portrait
</th>
<th width="16%">Name
</th>
<th width="15%">Start
</th>
<th width="15%">End
</th>
<th width="22%">Succession / Notes
</th>
<th width="15%">Title
</th>
<th width="4%">Ref.
</th></tr>

Let’s now try to find all the cells in the first row.

cells = first_row.find_all("th")
print (cells)
[<th width="10%">Portrait
</th>, <th width="16%">Name
</th>, <th width="15%">Start
</th>, <th width="15%">End
</th>, <th width="22%">Succession / Notes
</th>, <th width="15%">Title
</th>, <th width="4%">Ref.
</th>]

We can iterate over these cells and print off their text. I am adding .strip() here to remove the leading whitespaces and line breaks.

for cell in cells:
    print (cell.text.strip())
Portrait
Name
Start
End
Succession / Notes
Title
Ref.

17.6. Iterating Across the Entire Table

Now that we know how to grab all tables, all rows within a table, and all cells within a row, let’s try and iterate over the entire first table. First, let’s grab the headers.

rows = first_table.find_all("tr")
for row in rows:
    cells = row.find_all("th")
    for cell in cells:
        print (cell.text.strip())
Portrait
Name
Start
End
Succession / Notes
Title
Ref.

Now that we know all the headers, explore all the table data cells which start on the next row.

rows = first_table.find_all("tr")
for row in rows:
    cells = row.find_all("td")
    for cell in cells:
        print (cell.text.strip())
Charles II  "the Bald"[b]
c. 10 August 843[c][i]
6 October 877
• Son of Louis I and grandson of Charlemagne (Charles I)
King of the Franks(840–77)Emperor of the Romans (875–77)
[7][8][9][10]

Louis II "the Stammerer"[d]
6 October 877[ii]
10 April 879
• Son of Charles the Bald
King of the Franks
[8][7][11]

Louis III
10 April 879[iii]
5 August 882
• Son of Louis the Stammerer
King of the Franks
[8][12][13]

Carloman II
10 April 879[iv]
12 December 884
• Son of Louis the Stammerer
King of the Franks
[8][7][13]

Charles (III) "the Fat"[e]
12 December 884[v]
13 January 888
• Son of Louis II the German • Cousin once removed of Carloman II and Louis III • Grandson of Louis I
King of East Francia(871–88)Emperor of the Romans (881–88)
[14][15]

Eudes(Robertians)
13 January 888[vi]
1 January 898
• Son of Robert the Strong • Rivaled by Charles III.
King of the Franks
[16][11]

Charles III"the Simple"
1 January 898[vii]
29 June 922
• Posthumous son of Louis II • Deposed
King of the Franks
[8][7]

Robert I(Robertians)
29 June 922[viii]
15 June 923
• Son of Robert the Strong and younger brother of Odo •  Killed at the Battle of Soissons
King of the Franks
[17][11]

Rudolph(Bosonids)
15 June 923[ix]
14 January 936
• Son of Richard, Duke of Burgundy • Son-in-law of Robert I
King of the Franks
[8][7]

Louis IV"d'Outremer"
19 June 936[x]
10 September 954
• Son of Charles III the Simple
King of the Franks
[8][7]

Lothair[f]
10 September 954[xi]
2 March 986
• Son of Louis IV
King of the Franks
[8][18]

Louis V"the Do-Nothing"
2 March 986[xii]
22 May 987
• Son of Lothair
King of the Franks
[19][20]

Now that you know how to grab all of this data, it will be time to bring all of this together for your final task. In the next chapter, we will try and extract all this data into properly structured data that will be stored within Python, then parsed as a Pandas DataFrame, and finally saved as a .csv file.

17.7. Other Options

I want to emphasize that there are other, easier ways to do this task. As with all things in Python, there are multiple solutions to the same problem. Here is one potential solution with Pandas. Pandas has a .read_html() function that can take a url. It will return a list of all tables in an HTML document. Let’s try that and take a look at the first table.

import pandas as pd
df = pd.read_html(url)[0]
df
Portrait Name Start End Succession / Notes Title Ref.
0 NaN Charles II "the Bald"[b] c. 10 August 843[c][i] 6 October 877 • Son of Louis I and grandson of Charlemagne (... King of the Franks(840–77)Emperor of the Roman... [7][8][9][10]
1 NaN Louis II "the Stammerer"[d] 6 October 877[ii] 10 April 879 • Son of Charles the Bald King of the Franks [8][7][11]
2 NaN Louis III 10 April 879[iii] 5 August 882 • Son of Louis the Stammerer King of the Franks [8][12][13]
3 NaN Carloman II 10 April 879[iv] 12 December 884 • Son of Louis the Stammerer King of the Franks [8][7][13]
4 NaN Charles (III) "the Fat"[e] 12 December 884[v] 13 January 888 • Son of Louis II the German • Cousin once rem... King of East Francia(871–88)Emperor of the Rom... [14][15]
5 NaN Eudes(Robertians) 13 January 888[vi] 1 January 898 • Son of Robert the Strong • Rivaled by Charle... King of the Franks [16][11]
6 NaN Charles III"the Simple" 1 January 898[vii] 29 June 922 • Posthumous son of Louis II • Deposed King of the Franks [8][7]
7 NaN Robert I(Robertians) 29 June 922[viii] 15 June 923 • Son of Robert the Strong and younger brother... King of the Franks [17][11]
8 NaN Rudolph(Bosonids) 15 June 923[ix] 14 January 936 • Son of Richard, Duke of Burgundy • Son-in-la... King of the Franks [8][7]
9 NaN Louis IV"d'Outremer" 19 June 936[x] 10 September 954 • Son of Charles III the Simple King of the Franks [8][7]
10 NaN Lothair[f] 10 September 954[xi] 2 March 986 • Son of Louis IV King of the Franks [8][18]
11 NaN Louis V"the Do-Nothing" 2 March 986[xii] 22 May 987 • Son of Lothair King of the Franks [19][20]

It’s not bad. And for a quick result, this is great, but it is not perfect. Notice the brackets with the citations in the Start column and the circle at the lead of each string in the Succession column. We also likely do not need to know NaN for each Portrait column. In other words, while Pandas is a quick solution, it is good to know how to do a custom solution using requests and BeautifulSoup.