BeautifulSoup XML 解析 - 只有 Return 第一个结果

BeautifulSoup XML Parsing - Only Return First Result

我正在尝试使用 BeautifulSoup 解析以下 XML 文件。但是,只返回第一个结果。

给出以下 XML:

<?xml version="1.0"?>
<TransXChange>
    <StopPoints>     
        <AnnotatedStopPointRef>         
            <StopPointRef>StopPointRefOne</StopPointRef>
            <CommonName>CommonNameOne</CommonName>
            <Indicator>IndicatorOne</Indicator>
            <LocalityName>LocalityNameOne</LocalityName>
            <LocalityQualifier>LocalityQualifierOne</LocalityQualifier>
        </AnnotatedStopPointRef>
        <AnnotatedStopPointRef>  
            <StopPointRef>StopPointRefTwo</StopPointRef>
            <CommonName>CommonNameTwo</CommonName>
            <Indicator>IndicatorTwo</Indicator>
            <LocalityName>LocalityNameTwo</LocalityName>
            <LocalityQualifier>LocalityQualifierTwo</LocalityQualifier>
        </AnnotatedStopPointRef>
        <AnnotatedStopPointRef>          
            <StopPointRef>StopPointRefThree</StopPointRef>
            <CommonName>CommonNameThree</CommonName>
            <Indicator>IndicatorThree</Indicator>
            <LocalityName>LocalityNameThree</LocalityName>
            <LocalityQualifier>LocalityQualifierThree</LocalityQualifier>
        </AnnotatedStopPointRef>

以及以下 Python 脚本:

from bs4 import BeautifulSoup as bs

inputFile = open("sample.xml","r")

contents = inputFile.read()

soup = bs(contents, 'xml')

StopPoints = soup.find_all('StopPoints')

for annotatedStopPointRef in StopPoints:
    print(annotatedStopPointRef.StopPointRef.string)

我只得到以下结果: StopPointRefOne

我期望的地方: StopPointRefOneStopPointRefTwoStopPointRefThree

在您的脚本中,您只搜索 'StopPoints',只有一个。所以循环只会迭代一次。您还需要在循环内搜索 'AnnotatedStopPointRef'

soup = bs(contents, 'xml')

StopPoints = soup.find_all('StopPoints')

for sp in StopPoints:
    for annotatedStopPointRef in sp.find_all('AnnotatedStopPointRef'):
        print(annotatedStopPointRef.StopPointRef.string)

打印:

StopPointRefOne
StopPointRefTwo
StopPointRefThree