首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python导航lxml.objectify属性

Python导航lxml.objectify属性
EN

Stack Overflow用户
提问于 2017-10-16 17:56:35
回答 1查看 173关注 0票数 0

我在访问ObjectifiedElement及其属性的列表时遇到问题。我有一个包,它只给我一个ObjectifiedElement类型的列表,我尝试使用lxml.objectify.dump转换列表中的第一个元素

这个错误是不相关的,所以我将在这里删除。这是我用来输出下面对象的代码

代码语言:javascript
复制
print ("Images: ")
print (lxml.objectify.dump(self.product.images[0]))

这是输出;

代码语言:javascript
复制
{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的属性。

更新

我已经设法在一个循环中访问了对象属性。以下是代码;

代码语言:javascript
复制
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)

我得到了这个;

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

发布于 2017-10-16 19:46:43

你试过print self.product.images[0].HiResImage.URL吗?

编辑:

我模拟了转储的最小化对象,使其只包含您想要访问的元素

代码语言:javascript
复制
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)

作为结果给予:

代码语言:javascript
复制
{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:

代码语言:javascript
复制
>>> print o.HiResImage.URL
https://images-eu.ssl-images-amazon.com/images/I/71whgyNdlVL.jpg
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46767533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档