使用lxm从xml中查找不同的节点和值

2024-04-25 17:42:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道为什么lxml会解析我正在浏览的xml的一部分,而不是其他部分。你知道吗

下面的代码片段很有效,并提供了我需要的所有标题:

doc = lh.fromstring(resp)
for product in doc.xpath('.//item'):
    prices = product.xpath(".//title/text()")

不过,对

doc = lh.fromstring(resp)
for product in doc.xpath('.//item'):
    prices = product.xpath(".//itemId/text()")

或者

doc = lh.fromstring(resp)
for product in doc.xpath('.//item'):
    prices = product.xpath(".//globalId/text()")

只返回作为空结果数的价格。你知道吗

XML如下所示。。。你知道吗

<findItemsByProductResponse>
  <ack>Success</ack>
  <version>1.12.0</version>
  <timestamp>2013-02-04T13:35:57.106Z</timestamp>
  <searchResult count="31">
    <item>
      <itemId>130842622974</itemId>
      <title>BONES - COMPLETE SEASON 4 - BLURAY</title>
      <globalId>EBAY-US</globalId>
    <primaryCategory>
      <categoryId>617</categoryId>
      <categoryName>DVDs & Blu-ray Discs</categoryName>
    </primaryCategory>
    <galleryURL>
      http://thumbs3.ebaystatic.com/m/mnuTBPOWZ-6F4kIHS1mj3gg/140.jpg
    </galleryURL>
    <viewItemURL>
      http://www.ebay.com/itm/BONES-COMPLETE-SEASON-4-BLURAY-/130842622974?pt=US_DVD_HD_DVD_Blu_ray
    </viewItemURL>
    <productId type="ReferenceID">78523575</productId>
    <paymentMethod>PayPal</paymentMethod>
    <autoPay>false</autoPay>
    <postalCode>60544</postalCode>
    <location>Plainfield,IL,USA</location>
    <country>US</country>
    <shippingInfo>
      <shippingServiceCost currencyId="USD">0.0</shippingServiceCost>
      <shippingType>Free</shippingType>
      <shipToLocations>Worldwide</shipToLocations>
      <expeditedShipping>true</expeditedShipping>
      <oneDayShippingAvailable>false</oneDayShippingAvailable>
      <handlingTime>1</handlingTime>
    </shippingInfo>
    <sellingStatus>
      <currentPrice currencyId="USD">12.99</currentPrice>
      <convertedCurrentPrice currencyId="USD">12.99</convertedCurrentPrice>  
      <sellingState>Active</sellingState>
      <timeLeft>P3DT23H12M7S</timeLeft>
    </sellingStatus>
    <listingInfo>
      <bestOfferEnabled>false</bestOfferEnabled>
      <buyItNowAvailable>false</buyItNowAvailable>
      <startTime>2013-01-29T12:48:04.000Z</startTime>
      <endTime>2013-02-08T12:48:04.000Z</endTime>
      <listingType>FixedPrice</listingType>
      <gift>false</gift>
    </listingInfo>
    <returnsAccepted>true</returnsAccepted>
    <condition>
      <conditionId>1000</conditionId>
      <conditionDisplayName>Brand New</conditionDisplayName>
    </condition>
    <isMultiVariationListing>false</isMultiVariationListing>
    <topRatedListing>true</topRatedListing>
  </item>

另外,我将寻找convertedCurrentPrice作为下一步的目标(我只是认为我应该一次解开一个谜团)——我将要使用的代码看起来像

doc = lh.fromstring(resp)
for product in doc.xpath('.//item'):
    prices = product.xpath(".//sellingStatus/convertedCurrentPrice/text()")

这看起来是对的还是有更好的方法?你知道吗

谢谢你

马特


Tags: textinfalsefordoctitleproductitem
1条回答
网友
1楼 · 发布于 2024-04-25 17:42:44

尝试使用itemid,因为我认为lxml会将标记转换为小写。为什么不使用:

doc.xpath('.//item/itemid/text()")

相关问题 更多 >