【问题标题】:Extract text from row of HTML table using python使用python从HTML表格的行中提取文本
【发布时间】:2023-04-05 03:33:02
【问题描述】:

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

早上 7:20 第一道曙光 早上 7:56 日出

<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>

请原谅脏代码块

提前致谢。

【问题讨论】:

  • 但我无法弄清楚或找不到任何指导我的资源 您找不到任何关于解析和操作 HTML 的内容吗?你不是已经在使用 BeautifulSoup 了吗?

标签: python html web-scraping html-table data-extraction


【解决方案1】:

是的,您可以尝试像这样遍历 td 标签:

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

输出:

 7:20AM
 7:56AM 

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    相关资源
    最近更新 更多