BeaufifulSoup,标记名称中带有“-”的标记的lxml导航?

问题描述 投票:0回答:1
<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>

我的问题是要在上面通过导航类似”和”的标签。由于标签名称中包含“-”,因此“。”导航无法正常工作。另外,由于子标签具有相同的名称,因此我无法使用它直接查找。如何浏览和迭代“”标签下的标签?

python python-3.x beautifulsoup lxml
1个回答
0
投票

您可能可以这样操作:

© www.soinside.com 2019 - 2024. All rights reserved.