簡體   English   中英

python 2.7,xml,beautifulsoup4:僅返回匹配的父標記

[英]python 2.7, xml, beautifulsoup4: only return matching parent tag

我正在嘗試解析一些XML,但是在強制它僅選擇請求標簽(如果它是父標簽)時遇到了問題。 例如,我的XML的一部分是:

<Messages>
    <Message ChainCode="LI" HotelCode="5501" ConfirmationID="5501">
      <MessageContent>
        <OTA_HotelResNotifRQ TimeStamp="2014-01-24T21:02:43.9318703Z" Version="4" ResStatus="Book">
          <HotelReservations>
            <HotelReservation>
              <RoomStays>
                <RoomStay MarketCode="CC" SourceOfBusiness="CRS">
                  <RoomRates>
                    <RoomRate EffectiveDate="2014-02-04" ExpireDate="2014-02-06" RoomTypeCode="12112" NumberOfUnits="1" RatePlanCode="RAC">
                      <Rates>
                        <Rate EffectiveDate="2014-02-04" ExpireDate="2014-02-06" RateTimeUnit="Day" UnitMultiplier="3">
                          <Base AmountBeforeTax="749.25" CurrencyCode="USD" />
                          <Total AmountBeforeTax="749.25" CurrencyCode="USD" />
                        </Rate>
                      </Rates>
                    </RoomRate>
                  </RoomRates>
                  <Total AmountBeforeTax="2247.75" CurrencyCode="USD">
                    <Taxes Amount="0.00" />
                  </Total>
                </RoomStay>
              </RoomStays>
            </HotelReservation>
          </HotelReservations>
        </OTA_HotelResNotifRQ>
      </MessageContent>
    </Message>
  </Messages>

除了“ Total”標簽外,我已經對整個事情進行了解析,以了解我的需要。

我想要得到的總標簽是:

 <Total AmountBeforeTax="2247.75" CurrencyCode="USD">
     <Taxes Amount="0.00" />
 </Total>

發生的事情是,它返回的是“ RoomRates \\ RoomRate \\ Rates \\ Rate”子級的“ Total”標記。 我試圖弄清楚如何指定它以僅返回RoomStays \\ RoomStay \\ Total標記。 我目前擁有的是:

soup = bs(response, "xml")

messages = soup.find_all('Message')

for message in messages:
    hotel_code = message.get('HotelCode')

    reservations = message.find_all('HotelReservation')
    for reservation in reservations:
        uniqueid_id = reservation.UniqueID.get('ID')
        uniqueid_idcontext = reservation.UniqueID.get('ID_Context')

        roomstays = reservation.find_all('RoomStay')
        for roomstay in roomstays:

            total = roomstay.Total

關於如何指定我要拉的確切標簽的任何想法? 如果有人想知道for循環,那是因為通常有多個“消息”,“酒店預訂”,“客房住宿”等標記,但是我刪除了它們僅顯示了一個。 有時也可能有多個Rate \\ Rates標簽,所以我不能僅僅要求它給我第二個“總計”標簽。

希望我已經解釋了這個問題。

有時也可能有多個Rate \\ Rates標簽,所以我不能僅僅要求它給我第二個“總計”標簽。

為什么不只遍歷所有Total標記,而跳過沒有Taxes子項的標記呢?

reservations = message.find_all('HotelReservation')
for reservation in reservations:
    totals = reservation.find_all('Total')
    for total in totals:
        if total.find('Taxes'):
             # do stuff
        else:
             # these aren't the totals you're looking for

如果您通常希望消除那些沒有子節點的節點,則可以執行以下任一操作:

if next(total.children, None):
    # it's a parent of something

if total.contents:
    # it's a parent of something

或者,您可以使用函數而不是字符串作為過濾器

total = reservation.find(lambda node: node.name == 'Total' and node.contents)

或者,您也可以使用其他方法來定位此標簽:它是RoomStay的直接子代,而不僅僅是后代; 它不是Rate的后代; 這是RoomStay下的最后Taxes后代; 等等。所有這些都可以輕松完成。


話雖如此,對於XPath來說似乎是一項完美的工作, BeautifulSoup不支持XPath,但是ElementTreelxml做到……

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM