简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM