Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beautiful Soup and Tables

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>&nbsp;<a href="whatever" class="bcQLink">&nbsp;Q&nbsp;</a>&nbsp;<a href="chart.asp?sym=ABX&code=XDAILY" class="bcQLink">&nbsp;C&nbsp;</a>&nbsp;<a href="texpert.asp?sym=ABX&code=XDAILY" class="bcQLink">&nbsp;O&nbsp;</a>&nbsp;</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

like image 762
xDG Avatar asked Feb 24 '23 01:02

xDG


1 Answers

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:

html = file("whatever.html")
soup = BeautifulSoup(html)
rows = soup.findAll("tr", {'class': ['odd', 'even']})
dat = []
for row in rows:
  dat.append( map( str, row.findAll('td') )

The resulting dat variable is:

[['<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>&nbsp;<a href="whatever" class="bcQLink">&nbsp;Q&nbsp;</a>&nbsp;<a href="chart.asp?sym=ABX&amp;code=XDAILY" class="bcQLink">&nbsp;C&nbsp;</a>&nbsp;<a href="texpert.asp?sym=ABX&amp;code=XDAILY" class="bcQLink">&nbsp;O&nbsp;</a>&nbsp;</center></td>']]

Edit: wrong array index

like image 186
teambob Avatar answered Feb 25 '23 14:02

teambob