繁体   English   中英

Python导航lxml.objectify属性

[英]Python navigate lxml.objectify properties

我在访问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。

您是否尝试过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.jpg

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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