Reputation: 13
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?
Upvotes: 1
Views: 2104
Reputation: 22697
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
Upvotes: 4
Reputation: 473763
nth-of-type
CSS selector
would help here:
from bs4 import BeautifulSoup
data = """
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
"""
soup = BeautifulSoup(data)
for td in soup.select('table > tr > td:nth-of-type(2)'):
print td.text
Prints:
2
5
8
Upvotes: 4