查找选项卡内的所有链接

2024-03-28 13:13:33 发布

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

我的html页面有:

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

...

我有:

html = BeautifulSoup(page)

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

如何找到此表中的所有链接?


Tags: 链接htmlpagetable页面linkstrclass
3条回答

找到表(by class在本例中),然后找到其中的所有链接。

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

这将返回页面中的链接列表

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

使用SoupStrainer比原始查找更有效:

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

另请参见Search by Class documentation

相关问题 更多 >