有没有办法在 Python 中使用 查找非递归 DOM 子节点BeautifulSoup
?
例如考虑解析一个pom.xml
文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>com.parent</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>2.0.0</modelVersion>
<groupId>com.parent.somemodule</groupId>
<artifactId>some_module</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Some Module</name>
...
Run Code Online (Sandbox Code Playgroud)
如果我想进入groupId
顶级(特别是project->groupId
,不是project->parent->groupId
),我使用:
with open(pom) as pomHandle:
soup = BeautifulSoup(pomHandle)
groupId = soup.groupid.text
Run Code Online (Sandbox Code Playgroud)
但不幸的是,groupId
无论层次结构如何,它都会在文件中找到第一个物理出现的project->parent->groupId
. 我实际上只想在特定节点级别而不是在其子节点内进行非递归查找。有没有办法做到这一点BeautifulSoup
?