简体   繁体   English

如何在python中使用xpath提取xml中的特定节点

[英]how to extract a particular node in xml using xpath in python

I am using python-amazon product module to do item look up in amazon api. 我正在使用python-amazon产品模块在亚马逊api中进行项目查找。

It returns the xml object of this kind. 它返回这种xml对象。 I want to know how to use xpath to extract the hyperlink of <IFrameURL> . 我想知道如何使用xpath提取<IFrameURL>hyperlink

<ItemLookupResponse>
    <Items>
        <Item>
            <ASIN>0316067938</ASIN>
            <CustomerReviews>
                <IFrameURL>
                http://www.amazon.com/reviews/iframe?akid=[AWS Access Key ID]&asin=0316067938&exp=2011-08-01T17%3A54%3A07Z&linkCode=xm2&summary=0&tag=ws&truncate=256&v=2&sig=[Signature]
                </IFrameURL>
            </CustomerReviews>
        </Item>
    </Items>
</ItemLookupResponse>

I tried this: but it did not work: 我试过了:但是没有用:

result = api.item_lookup('0316067938', ResponseGroup='Reviews',
     TruncateReviewsAt=256, IncludeReviewsSummary=False)
print result.xpath('//IFrameURL')

but this gave empty list 但这给了空名单

EDIT: 编辑:

I get this when I do [x.tag for x in result.xpath('//*')] : 当我[x.tag for x in result.xpath('//*')]执行[x.tag for x in result.xpath('//*')]时得到了这个:

['{http://webservices.amazon.com/AWSECommerceService/2011-08-01}ItemLookupResponse', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}OperationRequest', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}HTTPHeaders', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Header', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}RequestId', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Arguments', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Argument', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Argument', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Argument', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Argument', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Argument', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Argument', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Argument', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Argument', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Argument', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Argument', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Argument', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}RequestProcessingTime', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Items', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Request', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}IsValid', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}ItemLookupRequest', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}IdType', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}ItemId', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}ResponseGroup', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}VariationPage', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}IncludeReviewsSummary', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}TruncateReviewsAt', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}Item', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}ASIN', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}CustomerReviews', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}IFrameURL', '{http://webservices.amazon.com/AWSECommerceService/2011-08-01}HasReviews']

Edit 2: I get this now as http://www.amazon.com/reviews/iframe?akid=HIDDEN&alinkCode=xm2&asin=B00062B6QY&atag=HIDDEN&exp=2014-01-28T07%3A55%3A45Z&summary=0&truncate=256&v=2&sig=HIDDEN%3D 编辑2:我现在以http://www.amazon.com/reviews/iframe?akid=HIDDEN&alinkCode=xm2&asin=B00062B6QY&atag=HIDDEN&exp=2014-01-28T07%3A55%3A45Z&summary=0&truncate=256&v=2&sig=HIDDEN%3D

query while ignoring the namespace 查询而忽略名称空间

>>> result.xpath('//*[local-name()="IFrameURL"]/text()')[0].strip()
'http://www.amazon.com/reviews/iframe?akid=[AWS Access Key ID]&asin=0316067938&exp=2011-08-01T17%3A54%3A07Z&linkCode=xm2&summary=0&tag=ws&truncate=256&v=2&sig=[Signature]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM