Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 BeautifulSoup如何从HTML表的特定列中提取数据。我的代码正在提取所有列_Python 2.7_Selenium Webdriver_Beautifulsoup - Fatal编程技术网

Python 2.7 BeautifulSoup如何从HTML表的特定列中提取数据。我的代码正在提取所有列

Python 2.7 BeautifulSoup如何从HTML表的特定列中提取数据。我的代码正在提取所有列,python-2.7,selenium-webdriver,beautifulsoup,Python 2.7,Selenium Webdriver,Beautifulsoup,我有一个带有行和列的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_regr

我有一个带有行和列的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代码段是:

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

全部的
93
93
0
0
如何提取“总计”“93”并在同一行中打印出来

谢谢,Riaz

您可以使用
find_all()
并对结果进行切片:

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

太好了。谢谢你的帮助。