[Tutor] Newbie question - syntax - BeautifulSoup

Tommy Kaas tommy.kaas at kaasogmulvad.dk
Fri Jul 30 08:36:07 CEST 2010


Thanks for the explanation. It's clearer now.
Tommy

"Tommy Kaas" <tommy.kaas at kaasogmulvad.dk> wrote

> > > for row in soup('table', {'class' : 'spad'})[0].tbody('tr'):
> >
> >Do you understand the syntax from a Python point of view?
>
> No. That's the problem.


OK, I'll assume you understand the basic for loop structure
and focus on the function call:

soup('table', {'class' : 'spad'})[0].tbody('tr')

Ignore the bit at the end for now:

soup('table', {'class' : 'spad'})

Thats a call to a function taking a string and a dictionary as 
arguments.
The string says we want to look for table tags. And the dictionary 
says
we want tables that have an attribute class with a value spad.
Is that bit clear?

Then we add an index [0] to get the first table.

Finally we call tbody("tr") to extract the tr tags from the table.
The for loop thus iterates over the rows of the first table with 
class=spad.

There might be slightly more to it than that, its a long time since I
played with BS...

>> Which help file?
>Well, maybe not a file, but the text produced by typing: 
>help(BeautifulSoup)

Ah, in that case you should definitely read the tutorial.

http://www.crummy.com/software/BeautifulSoup/documentation.html

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list