<body>
<response status="success">
<policy>
<shared/>
<panorama>
<address>
<entry name="text">
<tag1></tag1>>
<tag2></tag2>
</entry>>
</address>
<service>
....
</service>
<pre-rulebase>
</pre-rulebase>
<security>
<rules>
<entry name="some text">
<tag1>text</tag1>>
<tag2>text</tag2>
</entry>
<entry name="more text">
<tag1>text</tag1>
<tag2>text</tag2>
</entry>
...
</rules>
</security>
<post-rulebase>
<entry name="some text">
<tag1>text</tag1>>
<tag2>text</tag2>
</entry>
<entry name="more text">
<tag1>text</tag1>>
<tag2>text</tag2>
</entry>
</post-rulebase>
</panorama>
</policy>
</response>
</body>
嗨,
我正在尝试使用Python BeautifulSoup和lxml解析上述xml文件。通常,我使用'。'导航到元素。例如
from bs4 import BeautifulSoup
with open('sample.xml', 'r') as xml_file:
soup = BeautifulSoup(xml_file, 'lxml')
for item in soup.body.response.policy.panorama.address.find('entry'):
<some code action>
我的问题是要在上面通过导航类似”和”的标签。由于标签名称中包含“-”,因此“。”导航无法正常工作。另外,由于子标签具有相同的名称,因此我无法使用它直接查找。如何浏览和迭代“”标签下的标签?
您可能可以这样操作: