簡體   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