我在访问ObjectifiedElement及其属性的列表时遇到问题。我有一个包,它只给我一个ObjectifiedElement类型的列表,我尝试使用lxml.objectify.dump转换列表中的第一个元素
这个错误是不相关的,所以我将在这里删除。这是我用来输出下面对象的代码
print ("Images: ")
print (lxml.objectify.dump(self.product.images[0]))这是输出;
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}ImageSet = None [ObjectifiedElement]
* Category = 'variant'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}SwatchImage = None [ObjectifiedElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL = 'https://images-eu.ssl-images-amazon.com/images/I/41NqvDXw0rL._SL30_.jpg' [StringElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height = 17 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width = 30 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}SmallImage = None [ObjectifiedElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL = 'https://images-eu.ssl-images-amazon.com/images/I/41NqvDXw0rL._SL75_.jpg' [StringElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height = 42 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width = 75 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}ThumbnailImage = None [ObjectifiedElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL = 'https://images-eu.ssl-images-amazon.com/images/I/41NqvDXw0rL._SL75_.jpg' [StringElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height = 42 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width = 75 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}TinyImage = None [ObjectifiedElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL = 'https://images-eu.ssl-images-amazon.com/images/I/41NqvDXw0rL._SL110_.jpg' [StringElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height = 62 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width = 110 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}MediumImage = None [ObjectifiedElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL = 'https://images-eu.ssl-images-amazon.com/images/I/41NqvDXw0rL._SL160_.jpg' [StringElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height = 90 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width = 160 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}LargeImage = None [ObjectifiedElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL = 'https://images-eu.ssl-images-amazon.com/images/I/41NqvDXw0rL.jpg' [StringElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height = 281 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width = 500 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}HiResImage = None [ObjectifiedElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL = 'https://images-eu.ssl-images-amazon.com/images/I/71whgyNdlVL.jpg' [StringElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height = 1440 [IntElement]
* Units = 'pixels'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width = 2560 [IntElement]
* Units = 'pixels'我尝试在每个实例中获取URL的属性。
更新
我已经设法在一个循环中访问了对象属性。以下是代码;
for e in self.product.images[0].getchildren():
print("Tag: L1: "+e.tag)
for v in e.getchildren():
print("Tag: L2: "+v.tag+" : "+v.text)我得到了这个;
Tag: L1: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}SwatchImage
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL : https://images-eu.ssl-images-amazon.com/images/I/41F%2BAxAz4BL._SL30_.jpg
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height : 17
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width : 30
Tag: L1: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}SmallImage
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL : https://images-eu.ssl-images-amazon.com/images/I/41F%2BAxAz4BL._SL75_.jpg
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height : 42
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width : 75
Tag: L1: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}ThumbnailImage
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL : https://images-eu.ssl-images-amazon.com/images/I/41F%2BAxAz4BL._SL75_.jpg
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height : 42
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width : 75
Tag: L1: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}TinyImage
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL : https://images-eu.ssl-images-amazon.com/images/I/41F%2BAxAz4BL._SL110_.jpg
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height : 62
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width : 110
Tag: L1: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}MediumImage
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL : https://images-eu.ssl-images-amazon.com/images/I/41F%2BAxAz4BL._SL160_.jpg
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height : 90
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width : 160
Tag: L1: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}LargeImage
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL : https://images-eu.ssl-images-amazon.com/images/I/41F%2BAxAz4BL.jpg
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height : 281
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width : 500
Tag: L1: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}HiResImage
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL : https://images-eu.ssl-images-amazon.com/images/I/81i%2BN3GG5rL.jpg
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Height : 1440
Tag: L2: {http://webservices.amazon.com/AWSECommerceService/2013-08-01}Width : 2560现在,我正在尝试访问每个属性并在不使用循环的情况下获取HiResImage URL。
发布于 2017-10-16 19:46:43
你试过print self.product.images[0].HiResImage.URL吗?
编辑:
我模拟了转储的最小化对象,使其只包含您想要访问的元素
a = '''<f:ImageSet xmlns:f="http://webservices.amazon.com/AWSECommerceService/2013-08-01" Category = 'variant'>
<f:HiResImage xmlns:f="http://webservices.amazon.com/AWSECommerceService/2013-08-01">
<f:URL xmlns:f="http://webservices.amazon.com/AWSECommerceService/2013-08-01">https://images-eu.ssl-images-amazon.com/images/I/71whgyNdlVL.jpg</f:URL>
</f:HiResImage>
</f:ImageSet>'''
o = objectify.fromstring(a)
print objectify.dump(o)作为结果给予:
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}ImageSet = None [ObjectifiedElement]
* Category = 'variant'
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}HiResImage = None [ObjectifiedElement]
{http://webservices.amazon.com/AWSECommerceService/2013-08-01}URL = 'https://images-eu.ssl-images-amazon.com/images/I/71whgyNdlVL.jpg' [StringElement]这与你发布的内容相似。然后我可以像这样访问URL:
>>> print o.HiResImage.URL
https://images-eu.ssl-images-amazon.com/images/I/71whgyNdlVL.jpghttps://stackoverflow.com/questions/46767533
复制相似问题