Python 2.7 BeautifulSoup如何从HTML表的特定列中提取数据。我的代码正在提取所有列
我有一个带有行和列的HTML表。我想从文本为“Total”的列中提取数据,并从值为“93”的列中提取数据 就这两列,我想提取数据。我的代码正在从所有列中提取数据 例如,我的输出是: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
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> </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])
太好了。谢谢你的帮助。