Python 使用BeautifulSoup查找测试用例和结果

Python 使用BeautifulSoup查找测试用例和结果,python,beautifulsoup,Python,Beautifulsoup,我需要一个很好的方法来找到所有测试用例的名称以及html文件中每个测试用例的结果。我是新来的,需要一些好的建议 首先,我使用BeautifulSoup读取数据并对其进行修饰,然后将数据放入文件中: from bs4 import BeautifulSoup f = open('myfile','w') soup = BeautifulSoup(open("C:\DEV\debugkod\data.html")) fixedSoup = soup.prettify() fixedSoup = fi

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

首先,我使用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()
例如,当我在文件中检查prettify结果中的部分时,它将如下所示(该文件包括100个tc和结果):

有人知道如何使用BeautifulSoup执行此操作吗?

使用以下代码:

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
。查看这些字符是如何在html中结束的。

thx以获取帮助!此代码将导致以下错误:name=td.text.strip()AttributeError:'NoneType'对象没有属性'text',这意味着在
类中不存在类似
的标记。因此,您的真实文件与您提供的示例不同。你能把完整的文件放在例如pastebin上吗?看看我用这段代码把数据外置在外
soup=BeautifulSoup(打开(“C:\DEV\debugkod\data.html”))fixedSoup=soup.prettify()fixedSoup=fixedSoup.encode('utf-8')myfile.write(fixedSoup)
如果我在记事本中打开data.html文件,它会是这样的++
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