BeautifulSoup and Problem Tables

academicedgar at gmail.com academicedgar at gmail.com
Sat Sep 20 23:51:52 EDT 2008


Hi

I would appreciate some help.  I am trying to learn Python and want to
use BeautifulSoup to pull some data from tables.  I was really psyched
earlier tonight when I discovered that I could do this

from BeautifulSoup import BeautifulSoup
bst=file(r"c:\bstest.htm").read()
soup=BeautifulSoup(bst)
rows=soup.findAll('tr')
len(rows)
a=len(rows[0].findAll('td'))
b=len(rows[1].findAll('td'))
c=len(rows[2].findAll('td'))
d=len(rows[3].findAll('td'))
e=len(rows[4].findAll('td'))
f=len(rows[5].findAll('td'))
g=len(rows[6].findAll('td'))
h=len(rows[8].findAll('td'))
i=len(rows[9].findAll('td'))
j=len(rows[10].findAll('td'))
k=rows[1].findAll('td')[1].contents[0]


So here I am chortling to myself thinking this is too easy.  I know
that the data columns are in rows[0] and so I can learn some more
python to figure out how to create tuples so I can lable each data
item using the row and column headings plucked from the contents.

However, I discovered that my tables have inconsistent numbers of
rows.  Even though the tables look pretty.  It might be that the
column heading for the third column is "Apples" but the value for
"Apples" in the fourth row is not in the third position in the row but
the fourth.

Now I am reluctant to make any assumptions because the tables were
created inconsistently. What I mean is that in some tables if there is
no value for a row/column intersection then there is a blank line, in
other tables if there is no value for a row/column intersection then
the length of  k (as above) is 0.

I have been Googling for some insight into this and I have not been
successful finding anything. I would really appreciate any suggestions
or some direction about how to better describe the problem.



More information about the Python-list mailing list