简体   繁体   中英

Python navigate lxml.objectify properties

I am having problems accessing a list of ObjectifiedElement and their properties. I have a package that just gives me a list of ObjectifiedElement type I have tried to convert the first element in the list by using lxml.objectify.dump

The error is unrelated so I will remove here. This is the code I have used to output the object below

print ("Images: ")
print (lxml.objectify.dump(self.product.images[0]))

And this is the output;

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

I am trying to get the property of URL in each instance.

Update

I have managed to access the object properties in a loop. Here is the code;

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)

And I got this;

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

Now I am trying to access each of the properties and get the HiResImage URL without using the loop.

Have you tried print self.product.images[0].HiResImage.URL ?

EDIT:

I simulated a minimized object of your dump to have only the element you want to access

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)

Giving as a result:

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

Which is similar to what you posted. Then I can access to the URL like this:

>>> print o.HiResImage.URL
https://images-eu.ssl-images-amazon.com/images/I/71whgyNdlVL.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM