Sometimes, we want to parse table with Python BeautifulSoup.
In this article, we’ll look at how to parse table with Python BeautifulSoup.
How to parse table with Python BeautifulSoup?
To parse table with Python BeautifulSoup, we can use the find_all
method.
For instance, we write
data = []
table = soup.find('table', attrs={'class':'lineItemsTable'})
table_body = table.find('tbody')
rows = table_body.find_all('tr')
for row in rows:
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele])
to call soup.find
to find the table element with class lineItemsTable
.
Then we call find
with 'tbody'
to find the tbody element from the table.
Next, we call table_body.find_all
to find all tr elements.
Then we loop through the returned rows
with a for loop.
In it, we call find_all
again to find all the td elements in the tr element.
And then we put all the text content of each td element into a list.
And then we call data.append
to append the values into the data
list.
Conclusion
To parse table with Python BeautifulSoup, we can use the find_all
method.