【发布时间】:2017-12-13 13:52:44
【问题描述】:
我有一个来自ifort 代码覆盖率报告的 html 文件。该 html 文件包含许多行,如下所示:
<a name="l1" style="background-color: #ffffff"> module WriteOutput</a>
我在python中使用以下内容成功导入了文件:
from lxml import html
with open(SampleSourceFile, "r") as f:
page = f.read()
tree = html.fromstring(page)
然后我实际上能够使用以下 XPATH 语法获取所有 name 属性
tree.xpath(r'/html/body//a/@name')
我看到这提供了有趣的可能性。是否也可以提取<a>标签的内容?即在这种情况下使用 XPATH 的字符串“模块 WriteOutput”?
我还可以添加一些约束吗?例如,我只想用某个background-color 取回<a> 标签中的names。这些事情可能吗?
谢谢,
【问题讨论】:
-
你可以尝试
//a[@style="background-color: #ffffff"]/@name | //a[@name and @style="background-color: #ffffff"]/text()获取文本内容和name属性值