brain storm
brain storm

Reputation: 31252

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.

It returns the xml object of this kind. I want to know how to use xpath to extract the hyperlink of <IFrameURL>.

<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('//*')]:

['{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

Upvotes: 0

Views: 283

Answers (1)

Guy Gavriely
Guy Gavriely

Reputation: 11396

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]'

Upvotes: 1

Related Questions