如何用靓汤在多张桌子中只选一张

2024-04-19 05:39:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含4到5个不同表的html文档。我想要的是一个class=“data”属性。我不知道怎么让BeautifulSoup只返回那张表。在

soup = BeautifulSoup(myhtml)

t = soup.findAll('table', 'class="data"')
for table in t:
    rows = table.findAll('tr')
    for tr in rows:
        cols = tr.findAll('td')
        for td in cols:
            print td

如果我删除上面的“class=”data“',我会从每个表中得到结果。是否可以只选择class=“data”的一个。或者,有没有其他方法可以迭代这些表?在


Tags: in文档fordata属性htmltabletr
2条回答

class属性指定为字典,如下所示:

t = soup.findAll('table', {'class': 'data'})

如果使用bs4,则可以使用select方法使用CSS选择器:

^{pr2}$

查看以下代码:

t = soup.find_all('table', class_='data')

属性类需要under引用

相关问题 更多 >