antuan55 antuan55 - 1 month ago 4
Python Question

Python BeautifulSoup: Iterating over a table

I want to iterate over every TD of each TR tag. So, for example, if I get all tables:

trList = tbody.findAll('tr')


Later I want to get all the TD tags of each TR element separately.

Something like:

trList[0]:
td[0]
td[1] # I wanted to get this TD of every TR
td[2]

trList[1]:
td[0]
td[1] # this one as well
td[2]


In a normal situation, I'd get it with a nested loop.

Is it possible to do that?

Answer

Yes you can, use the same function findAll

trList = tbody.findAll('tr')
for tr in trList:
    tdList = tr.findAll('td')
    for td in tdList:
        // here you got each td