首页 > 解决方案 > 使用python从HTML表格的行中提取文本

问题描述

我正在尝试使用 Python 从下面的 HTML 表中提取日出时间(上午 7 点 56 分),我用漂亮的汤刮掉了它。这是第二行的“文本右”,但我无法弄清楚或找到任何指导我的资源,我是否将表格循环到第二行?

第一道曙光 7:20 AM 日出 7:56 AM

<table class="table table-sm table-striped table-inverse table-tide">
    <tr>
        <td><strong>First Light</strong></td>
        <td class="text-right"> 7:20AM</td>
    </tr>
    <tr>
        <td><strong>Sunrise</strong></td>
        <td class="text-right"> 7:56AM </td>
    </tr>
</table>

原谅脏代码块

提前致谢。

标签: pythonhtmlweb-scrapinghtml-tabledata-extraction

解决方案


是的,您可以尝试像这样迭代 td 标记:

for td in soup.find_all('td', attrs={"class":"text-right"}):
    print(td.text)

输出:

 7:20AM
 7:56AM 

推荐阅读