查找表格内的所有链接

3 投票
3 回答
10287 浏览
提问于 2025-04-16 11:29

我的html页面有:

...
<table class="t1" ..>
<tr><td> ... <a href="">...</a> ... <a href="">..</a>
</table>

...

我有:

html = BeautifulSoup(page)

links = html.findAll('a', ?????????)

我该如何找到这个表格里面的所有链接呢?

3 个回答

0

这段代码应该会返回页面中的链接列表。

html = BeautifulSoup(page)
links = html.findAll('a')
1

比起直接查找,使用 SoupStrainer 会更有效率:

html  = BeautifulSoup(page, parseOnlyThese=SoupStrainer('table', 't1' ) )
links = html.findAll('a')

另外,你可以查看 按类查找的文档

7

首先,找到这个表格(在这里是通过类名来查找),然后在表格里找到所有的链接。

html = BeautifulSoup(page)
table = html.find('table', 't1')
links = table.findAll('a')

撰写回答