【问题标题】:Find all links inside a table查找表内的所有链接
【发布时间】:2011-02-09 02:59:42
【问题描述】:

我的 html 页面有:

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

...

我有:

html = BeautifulSoup(page)

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

如何找到该表内的所有链接?

【问题讨论】:

  • 你研究过 Python HTML 解析器吗?
  • @Nick - BeautifulSoup 就是这样......

标签: python beautifulsoup


【解决方案1】:

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

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

【讨论】:

【解决方案2】:

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

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

另请参阅Search by Class documentation

【讨论】:

    【解决方案3】:

    这应该返回页面中的链接列表

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

    【讨论】:

    • 我不想要页面上的所有链接,只想要 class=t1 的表格内的链接
    猜你喜欢
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多