【问题标题】:Finding test case and result with BeautifulSoup使用 BeautifulSoup 查找测试用例和结果
【发布时间】:2014-09-23 10:37:58
【问题描述】:

我需要一种好方法来查找所有测试用例的名称以及 html 文件中每个测试用例的结果。我是 BeautifulSoup 的新手,需要一些好的建议。

首先我已经这样做了,使用 BeautifulSoup 读取数据并对其进行美化并将数据放入文件中:

from bs4 import BeautifulSoup
f = open('myfile','w')
soup = BeautifulSoup(open("C:\DEV\debugkod\data.html"))
fixedSoup = soup.prettify()
fixedSoup = fixedSoup.encode('utf-8')
f.write(fixedSoup)
f.close()

当我在文件中检查美化结果中的部分时,它会如下所示(该文件包括 100 个 tc 和结果):

<a name="1005">
  </a>
  <div class="Sequence">
   <div class="Header">
    <table class="Title">
     <tr>
      <td>
       IAA REQPROD 55 InvPwrDownMode - Shut down communication (Sequence)
      </td>
      <td class="ResultStateIcon">
       <img src="Resources/Passed.png"/>
      </td>
     </tr>
    </table>
    <table class="DynamicAttributes">
     <colgroup>
      <col width="20">
       <col width="30">
        <col width="20">
         <col width="30">
         </col>
        </col>
       </col>
      </col>
     </colgroup>
     <tr>
      <th>
       Start time:
      </th>
      <td>
       2014/09/23 09-24-31
      </td>
      <th>
       Stop time:
      </th>
      <td>
       2014/09/23 09-27-25
      </td>
     </tr>
     <tr>
      <th>
       Execution duration:
      </th>
      <td>
       173.461 sec.
      </td>
      *<th>
       Name:
      </th>
      <td>
       IAA REQPROD 55 InvPwrDownMode - Shut down communication
      </td>*
     </tr>
     <tr>
      <th>
       Library link:
      </th>
      <td>
      </td>
      <th>
       Creation date:
      </th>
      <td>
       2013/4/11, 8-55-57
      </td>
     </tr>
     <tr>
      <th>
       Modification date:
      </th>
      <td>
       2014/9/23, 9-27-25
      </td>
      <th>
       Author:
      </th>
      <td>
       cnnntd
      </td>
     </tr>
     <tr>
      <th>
       Hierarchy:
      </th>
      <td>
       IAA.  IAA REQPROD 55 InvPwrDownMode - Shut down communication
      </td>
      <td>
      </td>
      <td>
      </td>
     </tr>
    </table>
    <table class="StaticAttributes">
     <colgroup>
      <col width="20">
       <col width="80">
       </col>
      </col>
     </colgroup>
     <tr>
      <th>
       Description:
      </th>
      <td>
      </td>
     </tr>
     <tr>
      <th>
       *Result state:
      </th>
      <td>
       Passed
      </td>*
     </tr>
    </table>
   </div>
   <div class="BlockReport">
    <a name="1007">

在这个文件中,我现在想找到关于“名称”和“结果状态:”的信息。如果检查美化结果,我可以看到标签“名称:”和“结果状态:”。希望可以使用它们来查找 testCase 名称和测试结果......所以打印输出应该是这样的:

 Name = IAA REQPROD 55 InvPwrDownMode - Shut down communication 
 Result = Passed
 etc

有谁知道如何使用 BeautifulSoup 做到这一点?

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    使用html from your second Pastebin link,代码如下:

    from bs4 import BeautifulSoup
    
    soup = BeautifulSoup(open("beautifulsoup2.html"))
    
    
    names = []
    for table in soup.findAll('table', attrs={'class': 'Title'}):
        td = table.find('td')
        names.append(td.text.encode("ascii", "ignore").strip())
    
    results = []
    for table in soup.findAll(attrs={'class': 'StaticAttributes'}):
        tds = table.findAll('td')
        results.append(tds[1].text.strip())
    
    for name, result in zip(names, results):
        print "Name = {}".format(name)
        print "Result = {}".format(result)
        print
    

    给出这个结果:

    Name = IEM(Project)
    Result = PassedFailedUndefinedError
    
    Name = IEM REQPROD 132765 InvPwrDownMode - Shut down communication SN1(Sequence)
    Result = Passed
    
    Name = IEM REQPROD 86434 InvPwrDownMode - Time from shut down to sleep SN2(Sequence)
    Result = PassedUndefined
    
    Name = IEM Test(Sequence)
    Result = Failed
    
    Name = IEM REQPROD 86434 InvPwrDownMode - Time from shut down to sleep(Sequence)
    Result = Error
    

    我添加了encode("ascii", "ignore"),否则我会得到UnicodeDecodeError。请参阅 this answer 了解这些字符如何最终出现在您的 html 中。

    【讨论】:

    • 感谢您的帮助!此代码将导致以下错误:name = td.text.strip() AttributeError: 'NoneType' object has no attribute 'text'
    • 这意味着没有像&lt;table class="Title"&gt; 这样具有class 属性Title 的标签。因此,您的真实文件与您提供的样本不同。你能把完整的文件放在例如 pastebin 上吗?
    • 签出pastebin.com/d6wzzxzW我用这段代码提取数据soup = BeautifulSoup(open("C:\DEV\debugkod\data.html")) fixedSoup = soup.prettify() fixedSoup = fixedSoup.encode('utf-8') myfile.write(fixedSoup)
    • 如果我只是在记事本++中打开 data.html 文件,它将看起来像这样pastebin.com/0vpJ2RGE
    猜你喜欢
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 2021-05-06
    • 1970-01-01
    相关资源
    最近更新 更多