如何在python中使用xpath提取xml中的特定节点
我正在使用AmazonAPI进行项目查找 它返回这种类型的如何在python中使用xpath提取xml中的特定节点,python,xml,xpath,amazon-web-services,Python,Xml,Xpath,Amazon Web Services,我正在使用AmazonAPI进行项目查找 它返回这种类型的xml对象。我想知道如何使用xpath提取的超链接 但这张单子是空的 编辑: 当我执行[result.xpath('/*')中x的x.tag]时,我得到了这个结果: ['{http://webservices.amazon.com/AWSECommerceService/2011-08-01}ItemLookupResponse','{http://webservices.amazon.com/AWSECommerceService/20
xml
对象。我想知道如何使用xpath
提取
的超链接
但这张单子是空的
编辑:
当我执行[result.xpath('/*')中x的x.tag]
时,我得到了这个结果:
['{http://webservices.amazon.com/AWSECommerceService/2011-08-01}ItemLookupResponse','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}操作请求','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}HTTPHeaders','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}标题“,”{http://webservices.amazon.com/AWSECommerceService/2011-08-01}请求ID','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}参数','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}论点','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}论点','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}论点','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}论点','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}论点','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}论点','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}论点','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}论点','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}论点','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}论点','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}论点','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}请求处理时间','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}项目','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}请求','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}是有效的','{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}响应组','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}变量页','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}包括查看摘要','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}截断视图卫星','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}项目','{http://webservices.amazon.com/AWSECommerceService/2011-08-01}阿辛','{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&alinkCode=xm2&asin=B00062B6QY&atag=HIDDEN&exp=2014-01-28T07%3A55%3A45Z&summary=0&truncate=256&v=2&sig=HIDDED%3D
忽略命名空间时进行查询
>>> 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]'
我得到索引器:列表索引超出范围结果。xpath('//iframeurl/text()')
正在返回空列表[x.tag for x in result.xpath('//*')]
若要查看其中的内容,我已将我在上面的编辑中得到的内容包括在内。我确实看到了iframeurl
谢谢。这很有帮助,但你能解释一下你在上面做了什么吗?我已经粘贴了我得到的文本。现在我必须解析它以获得评论。
result = api.item_lookup('0316067938', ResponseGroup='Reviews',
TruncateReviewsAt=256, IncludeReviewsSummary=False)
print result.xpath('//IFrameURL')
>>> 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]'