【问题标题】:how to extract a particular node in xml using xpath in python如何在python中使用xpath提取xml中的特定节点
【发布时间】:2014-02-17 18:05:28
【问题描述】:

我正在使用 python-amazon product module 在 amazon api 中查找项目。

它返回这种类型的xml 对象。我想知道如何使用xpath提取hyperlink<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>

我试过这个: 但它不起作用:

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

但这给出了空列表

编辑:

当我做[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']

编辑 2: 我现在得到这个http://www.amazon.com/reviews/iframe?akid=HIDDEN&amp;alinkCode=xm2&amp;asin=B00062B6QY&amp;atag=HIDDEN&amp;exp=2014-01-28T07%3A55%3A45Z&amp;summary=0&amp;truncate=256&amp;v=2&amp;sig=HIDDEN%3D

【问题讨论】:

    标签: python xml xpath amazon-web-services


    【解决方案1】:

    忽略命名空间的查询

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

    【讨论】:

    • 我收到IndexError: list index out of range
    • result.xpath('//iframeurl/text()') 正在返回空列表
    • [x.tag for x in result.xpath('//*')] 看看里面有什么
    • 我在上面的编辑中包含了我得到的内容。我确实在那里看到了iframeurl
    • 谢谢。这有帮助,但你能解释一下你在上面做了什么吗?我已经粘贴了我出来的文字。现在我必须解析这个以获得评论。
    猜你喜欢
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    相关资源
    最近更新 更多