473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

BeautifulSoup and Problem Tables

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.
Sep 21 '08 #1
2 2600
On Sat, 20 Sep 2008 20:51:52 -0700 (PDT), ac***********@gmail.com wrote:
[snip]
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]
[snip]
However, I discovered that my tables have inconsistent numbers of
rows.
[snip]
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.
Would it be accurate to describe the problem as wanting to
extract the contents of the cth column of the rth row of a
table in spite of various pathologies in the construction of
the table?

If so, maybe it would help to post sample HTML (trimmed to a
minimum) of the pathologies that must be handled. I gotta
confess, though, that it doesn't take many rowspans or colspans
to put this problem beyond my reach.

--
To email me, substitute nowhere->spamcop, invalid->net.
Sep 21 '08 #2
ac***********@gmail.com wrote:
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.
This may help, and it may not.

One of the most useful features of BeautifulSoup is the .prettify()
function. Use it, and you can examine your xml with a browser or
decent editor. You will be able, hopefully, to see attributes that
may explain your difficulties. The one that snuck up and bit me was
table:number-columns-repeated -- I don't know what is biting you,
but it is weird the way known cells are completely skipped with this
attribute -- you have to know about it and check for it.

If there is a way to deal with that attribute using BeautifulSoup I
was unable to find it given the documentation and examples available
to me. I took a step back and tried xml.sax -- it is straighforward
and easy to use -- you build an xml.sax.handler for your xml, and it
deals with it.

It worked for me, but, as always, YMMV

sc
Sep 22 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ted | last post by:
Hi, I'm using the BeautifulSoup module and having some trouble processing a file. It's not printing what I'm expecting. In the code below, I'm expecting cells with only "bgcolor" attributes to...
7
by: Gonzillaaa | last post by:
I'm trying to get the data on the "Central London Property Price Guide" box at the left hand side of this page http://www.findaproperty.com/regi0018.html I have managed to get the data :) but...
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&". Is there some way I can get...
5
by: John Nagle | last post by:
This, which is from a real web site, went into BeautifulSoup: <param name="movie" value="/images/offersBanners/sw04.swf?binfot=We offer fantastic rates for selected weeks or days!!&blinkt=Click...
9
by: Mizipzor | last post by:
Is there a way to "subscribe" to individual topics? im currently getting bombarded with daily digests and i wish to only receive a mail when there is activity in a topic that interests me. Can this...
11
by: John Nagle | last post by:
The syntax that browsers understand as HTML comments is much less restrictive than what BeautifulSoup understands. I keep running into sites with formally incorrect HTML comments which are parsed...
2
by: Frank Stutzman | last post by:
I've got a simple script that looks like (watch the wrap): --------------------------------------------------- import BeautifulSoup,urllib ifile =...
5
by: Larry Bates | last post by:
Info: Python version: ActivePython 2.5.1.1 Platform: Windows I wanted to install BeautifulSoup today for a small project and decided to use easy_install. I can install other packages just...
3
by: Magnus.Moraberg | last post by:
Hi, I wish to extract all the words on a set of webpages and store them in a large dictionary. I then wish to procuce a list with the most common words for the language under consideration. So,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.