我有一個從html格式的鼻子的測試報告文件。 我想在Python中從文本中提取部分文本。我將通過郵件中的電子郵件發送該郵件。Python如何從html文件中提取內容
我有以下樣品:
<!DOCTYPE html>
<html>
<head>
<title>Unit Test Report</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style>
body {
font-family: Calibri, "Trebuchet MS", sans-serif;
}
* {
word-break: break-all;
}
table, td, th, .dataid {
border: 1px solid #aaa;
border-collapse: collapse;
background: #fff;
}
section {
background: rgba(0, 0, 0, 0.05);
margin: 2ex;
padding: 1ex;
border: 1px solid #999;
border-radius: 5px;
}
h1 {
font-size: 130%;
}
h2 {
font-size: 120%;
}
h3 {
font-size: 100%;
}
h4 {
font-size: 85%;
}
h1, h2, h3, h4, a[href] {
cursor: pointer;
color: #0074d9;
text-decoration: none;
}
h3 strong, a.failed {
color: #ff4136;
}
.failed {
color: #ff4136;
}
a.success {
color: #3d9970;
}
pre {
font-family: 'Consolas', 'Deja Vu Sans Mono',
'Bitstream Vera Sans Mono', 'Monaco',
'Courier New', monospace;
}
.test-details,
.traceback {
display: none;
}
section:target .test-details {
display: block;
}
</style>
</head>
<body>
<h1>Overview</h1>
<section>
<table>
<tr>
<th>Class</th>
<th class="failed">Fail</th>
<th class="failed">Error</th>
<th>Skip</th>
<th>Success</th>
<th>Total</th>
</tr>
<tr>
<td>Regression_TestCase.RegressionProject_TestCase2.RegressionProject_TestCase2</td>
<td class="failed">1</td>
<td class="failed">9</td>
<td>0</td>
<td>219</td>
<td>229</td>
</tr>
<tr>
<td><strong>Total</strong></td>
<td class="failed">1</td>
<td class="failed">9</td>
<td>0</td>
<td>219</td>
<td>229</td>
</tr>
</table>
</section>
<h1>Failure details</h1>
<section>
<h2>Regression_TestCase.RegressionProject_TestCase2.RegressionProject_TestCase2 (1 failures, 9 errors)</h2>
<div>
<section id="Regression_TestCase.RegressionProject_TestCase2.RegressionProject_TestCase2:test_00010_import_user_invalid_credentials">
<h3>test_00010_import_user_invalid_credentials: <strong>selenium.common.exceptions.NoSuchElementException</strong></h3>
<div class="test-details">
<h4>Traceback</h4>
<pre class="traceback">Traceback (most recent call last):
File "C:\Python27\lib\unittest\case.py", line 329, in run
testMethod()
File "C:\test_runners\selenium_regression_test_5_1_1\ClearCore - Regression Test\Regression_TestCase\RegressionProject_TestCase2.py", line 221, in test_00010_import_user_invalid_credentials
Globals.login_password_invalid)
File "C:\test_runners\selenium_regression_test_5_1_1\ClearCore - Regression Test\Pages\security.py", line 51, in enter_invalid_userid_and_password
self.enter_user_id(userid)
File "C:\test_runners\selenium_regression_test_5_1_1\ClearCore - Regression Test\Pages\security.py", line 32, in enter_user_id
user_id_element = self.get_element(*MainPageLocators.security_user_id_textfield_xpath)
File "C:\test_runners\selenium_regression_test_5_1_1\ClearCore - Regression Test\Pages\base.py", line 40, in get_element
element = self.driver.find_element(by=how, value=what)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 712, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Message: Unable to find element with xpath == //span[@class="gwt-InlineLabel marginbelow myinlineblock" and contains(text(), "User ID (including domain)")]/following-sibling::input
-------------------- >> begin captured stdout << ---------------------
*** Test import_invalid_user_credentials ***
05_12_1616_49_42
//span[@class="gwt-InlineLabel marginbelow myinlineblock" and contains(text(), "User ID (including domain)")]/following-sibling::input
Element not found
Message: Unable to find element with xpath == //span[@class="gwt-InlineLabel marginbelow myinlineblock" and contains(text(), "User ID (including domain)")]/following-sibling::input
05_12_1616_51_54
--------------------- >> end captured stdout << ----------------------
----
# There is more html below. I have not included everything. It will be too long otherwise.
如果我在瀏覽器中打開該文件的格式如下: 這是我想從HTML文件中提取文本。
Class Fail Error Skip Success Total
Regression_TestCase 1 9 0 219 229
我該怎麼做?以表格格式保存會很好。 謝謝,Riaz
你有沒有嘗試過任何與XML解析庫? (如https://docs.python.org/2.7/library/xml.etree.elementtree.html#module-xml.etree.ElementTree) – zezollo
我正在查找美麗的湯http://stackoverflow.com/questions/16835449/python-beautifulsoup-extract-text-between-element –
你想輸出的格式是什麼?你是否希望它看起來像excel中的表格(例如csv),還是你想要一個具有這些行和列以及間距的文本文件? – kaisquared