【问题标题】:BeautifulSoup How do I extract data from specific columns from HTML table. My code is extracting all the columnsBeautifulSoup 如何从 HTML 表的特定列中提取数据。我的代码正在提取所有列
【发布时间】:2017-03-11 05:14:29
【问题描述】:

我有一个包含一行和几列的 HTML 表格。我想从文本为“Total”的列中提取数据,从值为“93”的列中提取数据 只是这两列我想提取数据。我的代码正在从所有列中提取数据。

例如我的输出是:

Total
93
93
0
0

我想要的输出是:

Total 93

我的代码是:

def extract_total_from_report_htmltestrunner(): 
    filename = (
    r"C:\test_runners 2 edit project\selenium_regression_test\TestReport\ClearCore_Automated_GUI_Regression_TestReport.html")
    html_report_part = open(filename, 'r')
    soup = BeautifulSoup(html_report_part, "html.parser")
    tr_total_row = soup.find('tr', {'id': 'total_row'})
    tr_total_row.find(text=True, recursive=False)
    print tr_total_row.text
    return tr_total_row.text

HTML sn-p 是:

<table id='result_table'>
    <tr id='total_row'>
        <td>Total</td>
        <td>93</td>
        <td>93</td>
        <td>0</td>
        <td>0</td>
        <td>&nbsp;</td>
    </tr>
</table>

如何提取“Total”“93”并在同一行打印出来?

谢谢,里亚兹

【问题讨论】:

    标签: python-2.7 selenium-webdriver beautifulsoup


    【解决方案1】:

    您可以使用find_all()切片结果:

    " ".join(td.get_text(strip=True) for td in tr_total_row.find_all("td")[:2])
    

    【讨论】:

    • 太好了。感谢您的帮助。
    猜你喜欢
    • 2016-06-20
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2021-07-08
    • 2021-03-08
    • 1970-01-01
    相关资源
    最近更新 更多