Hi I’m trying to parse an html table using Beautiful Soup.
The table looks something like this:
<table width=100% border=1 cellpadding=0 cellspacing=0 bgcolor=#e0e0cc>
<tr>
<td width=12% height=1 align=center valign=middle bgcolor=#e0e0cc bordercolorlight=#000000 bordercolordark=white> <b><font face="Verdana" size=1><a href="http://www.dailystocks.com/" alt="DailyStocks.com" title="Home">Home</a></font></b></td>
</tr>
</table>
<table width="100%" border="0" cellpadding="1" cellspacing="1">
<tr class="odd"><td class="left"><a href="whatever">ABX</a></td><td class="left">Barrick Gold Corp.</td><td>55.95</td><td>55.18</td><td class="up">+0.70</td><td>11040601</td><td>70.28%</td><td><center> <a href="whatever" class="bcQLink"> Q </a> <a href="chart.asp?sym=ABX&code=XDAILY" class="bcQLink"> C </a> <a href="texpert.asp?sym=ABX&code=XDAILY" class="bcQLink"> O </a> </center></td></tr>
</table>
I would like to get the information from the second table, and so far I tried this code:
html = file("whatever.html")
soup = BeautifulSoup(html)
t = soup.find(id='table')
dat = [ map(str, row.findAll("td")) for row in t.findAll("tr") ]
That doesnt seem to work, any help would be much appreciated,
Thanks
The first problem is with this statement: “t=soup.find(id=’table’)” There is nothing with an id of table. I think what you mean is “t=soup.find(‘table’)” this finds a table. Unfortunately it only finds the first table.
You could do “t=soup.findAll(table)[1]” but this would be quite brittle.
I would suggest something like the following:
The resulting dat variable is:
Edit: wrong array index