BeautifulSoup HTML extracting tabular data
beautifulsoup, extract, python
Solution
In [70]: from bs4 import BeautifulSoup
In [71]: soup = BeautifulSoup(html)
In [72]: [tr.find('td').text for tr in soup.findAll('tr')]
Out[72]: [u'Euro', u'Australian dollar', u'Canadian dollar']
for the last items:
In [73]: [tr.findAll('td')[-1].text for tr in soup.findAll('tr')]
Out[73]: [u'111.6092', u'85.4785', u'82.1079']
Problem
I have this HTML table, and I need some data from it: ``` <table id="curFullTable" class="tablesorter" style="width:728px;margin-left:auto;margin-right:auto;"> <tr> <td>Euro</td> <td align="center">EUR</td> <td align="center">€</td> <td align="center">1</td> <td align="center">110.9416</td> <td align="center">111.2754</td> <td align="center">111.6092</td> </tr> <tr> <td>Australian dollar</td> <td align="center">AUD</td> <td align="center">$</td> <td align="center">1</td> <td align="center">84.9671</td> <td align="center">85.2228</td> <td align="center">85.4785</td> </tr> <tr> <td>Canadian dollar</td> <td align="center">CAD</td> <td align="center">$</td> <td align="center">1</td> <td align="center">81.6167</td> <td align="center">81.8623</td> <td align="center">82.1079</td> </tr> </table> ``` With this code: ``` tableData = htmlText.find("table", attrs={"class":"tablesorter"}) rows = tableData.findAll('tr') ``` I get all table rows and table cells in one list. So far I have managed to extract one by one currency name, but I really need a list of currency names, like this ``` currencies = ['Euro','Australian dollar','Canadian dollar'] ``` What would be the way to achieve this? Later on, I would also need to extract data from the last TD element of each TR element.