我弄清楚爲什麼lxml會解析我正在查看的xml的一部分,但不是其他位。使用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()")
不這看起來是正確的還是有更好的方法來做到這一點?
感謝,
馬特
看起來你的XML缺少結束標籤:) –