-
解析html
-
解析xml
import xml.dom.minidom
tree = xml.dom.minidom.parse('output.xml')
robot = tree.documentElement
root_suite = robot.childNodes
statistics = robot.getElementsByTagName('statistics')
statistics_suite = statistics[0].getElementsByTagName('suite')
statistics_suite_stat = statistics_suite[0].getElementsByTagName('stat')
suite_name = statistics_suite_stat[0].getAttribute('name')
statistics_suite_pass = statistics_suite_stat[0].getAttribute('pass')
statistics_suite_fail = statistics_suite_stat[0].getAttribute('fail')
# 打印dom类型的xml
# pretty_print = lambda f: '
'.join([line for line in md.parse(open(f)).toprettyxml(indent=' '*2).split('
') if line.strip()])
# 打印str类型的xml ,需要修改pretty_print 函数,只能打印dom 非test对象
pretty_print = lambda data: '
'.join([line for line in xml.dom.minidom.parseString(data).toprettyxml(indent=' '*2).split('
') if line.strip()])
# [<DOM Text node "'
'">, <DOM Element: suite at 0x99d8928>, ...
# print(root_suite)
#[<DOM Element: statistics at 0x81739b8>]
# print(statistics)
# 'NodeList' object has no attribute 'toprettyxml'
# print(statistics.toprettyxml())
# for i in statistics:
# # print(i.toprettyxml()) # 空格没有去掉
# print(pretty_print(i.toprettyxml()))
# 打印指定的dom
# print(pretty_print(root_suite[5].toprettyxml()))
#Test Statistics 数据
# for i in statistics_suite:
# print('*'*100)
# print(pretty_print(i.toprettyxml()))
#
# for i in statistics_suite:
# print('*'*100)
# print(pretty_print(i.toprettyxml()))
#
# for i in statistics_suite_stat:
# print('*'*100)
# print(pretty_print(i.toprettyxml()))
#pass 的数据
# for i in statistics_suite_pass:
# print('*' * 100)
# print(print(i))
# test
# test=robot.getElementsByTagName('test') # dom list 对象,只有一个
# print(test)
# for i in test:
# print('*'*100)
# print(pretty_print(i.toprettyxml()))
# test
# msgs = robot.getElementsByTagName('msg') # dom list 对象
# for i in msgs:
# # print('*'*100)
# # print(pretty_print(i.toprettyxml()))
# if i.childNodes[0]:
# # print(pretty_print(i.toprettyxml()))
# # print(i.childNodes[0])
# # print(pretty_print(i.childNodes[0].toprettyxml())) # 会报错,需要修改pretty_print 函数,只能打印dom 非test对象
# # print(i.childNodes[0].toprettyxml()) # 不会报错,不好
# print(i.childNodes[0].data) # 不会报错,最佳方法