命名空间问题。
您可以将命名空间放在所有项目的前面,如第一个答案中所述 这个问题 要么 这个问题 。一个可能更简单的解决方案是使用这样的快速黑客忽略命名空间:
xml_hacked_namespace = raw_xml.replace(' xmlsn=', ' xmlnamespace=') doc = fromstring(xml_hacked_namespace) item_list = doc.findall('.//Item')
如果您发现自己正在使用xml进行大量工作,那么您可能也有兴趣查看 LXML 。它更快,并提供了一些额外的方法,有些人觉得很好。
这是名称空间问题。这有效:
from xml.etree import ElementTree as ET XML = """<?xml version="1.0" ?> <ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2010-11-01"> <OperationRequest> <HTTPHeaders> <Header Name="UserAgent" Value="TSN (Language=Python)"></Header> </HTTPHeaders> <RequestId>96ef9bc3-68a8-4bf3-a2c7-c98b8aeae00f</RequestId> <Arguments> <Argument Name="Operation" Value="ItemLookup"></Argument> <Argument Name="Service" Value="AWSECommerceService"></Argument> <Argument Name="Signature" Value="gjc4wRNum3YT82app1d06vMIDM7v44fOmZTP8Uh3LqE="></Argument> <Argument Name="AssociateTag" Value="sneakick-20"></Argument> <Argument Name="Version" Value="2010-11-01"></Argument> <Argument Name="ItemId" Value="810056013349,810056013264"></Argument> <Argument Name="IdType" Value="UPC"></Argument> <Argument Name="AWSAccessKeyId" Value="AKIAIFMUMJLJOOINRVRA"></Argument> <Argument Name="Timestamp" Value="2012-01-03T21:26:39Z"></Argument> <Argument Name="ResponseGroup" Value="ItemIds"></Argument> <Argument Name="SearchIndex" Value="Apparel"></Argument> </Arguments> <RequestProcessingTime>0.0595830000000000</RequestProcessingTime> </OperationRequest> <Items> <Request> <IsValid>True</IsValid> <ItemLookupRequest> <IdType>UPC</IdType> <ItemId>810056013349</ItemId> <ItemId>810056013264</ItemId> <ResponseGroup>ItemIds</ResponseGroup> <SearchIndex>Apparel</SearchIndex> <VariationPage>All</VariationPage> </ItemLookupRequest> </Request> <Item> <ASIN>B000XR4K6U</ASIN> </Item> <Item> <ASIN>B000XR2UU8</ASIN> </Item> </Items> </ItemLookupResponse>""" NS = "{http://webservices.amazon.com/AWSECommerceService/2010-11-01}" doc = ET.fromstring(XML) Item_elems = doc.findall(".//" + NS + "Item") # All Item elements in document print Item_elems
输出:
[<Element '{http://webservices.amazon.com/AWSECommerceService/2010-11-01}Item' at 0xbf0c50>, <Element '{http://webservices.amazon.com/AWSECommerceService/2010-11-01}Item' at 0xbf0cd0>]
更接近您自己的代码:
NS = "{http://webservices.amazon.com/AWSECommerceService/2010-11-01}" doc = ET.fromstring(XML) items = doc[1] # Items element first_item = items.find(NS + 'Item') # First direct Item child all_items = items.findall(NS + 'Item') # List of all direct Item children