让我们考虑一个示例XML文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<feats>
<feat>
<name>Blindsight, 5-Ft. Radius</name>
<type>General</type>
<multiple>No</multiple>
<stack>No</stack>
<prerequisite>Base attack bonus +4, Blind-Fight, Wisdom 19.</prerequisite>
<benefit><div topic="Benefit" level="8"><p><b>Benefit:</b> Using senses such as acute hearing and sensitivity to vibrations, you detect the location of opponents who are no more than 5 feet away from you. <i>Invisibility</i> and <i>darkness</i> are irrelevant, though it you discern incorporeal beings.</p><p/>
</div>
</benefit>
<full_text>
<div topic="Blindsight, 5-Ft. Radius" level="3">Lorem ipsum
</div>
</div>
</full_text>
<reference>SRD 3.5 DivineAbilitiesandFeats</reference>
</feat>
</feats>
我想从<benefit>
标记中获取一个文本作为字符串,但是没有<div>
标记(<p>
和{
我设法得到了整个<div>
元素,但是当我使用.text
属性从中获取字符串时,它给出了mo None
。在
tree = ET.parse(filename)
root = tree.getroot()
data={}
for item in root.findall('feat'):
data["benefit"]=""
element = item.find('benefit').find("div")
print element.text
有一个简单的方法来得到这篇文章还是我必须为它编写特殊的函数?在
使用
lxml
可以首先找到<b>
元素,得到tail
并将其与以下同级元素组合以生成所需的结果,例如:输出:
^{pr2}$或者,如果您想简单地获取}:
<p>
的全部内容,包括其中的标记,那么您可以执行this way(这个方法可以使用lxml
或{输出:
不过,我同意马特关于美妆的看法
我在你的代码片段中添加了一些正则表达式,结果很好
相关问题 更多 >
编程相关推荐