亚马逊MWS Boto解析了XML缺失值

时间:2016-06-18 22:31:48

标签: python boto amazon-mws

Boto 2.40,Python 3.5

在查询亚马逊MWS get_competitive_pricing_for_asin时,一旦Boto解析了原始XML中存在的某些值,就会丢失。

原始XML包含NumberOfOfferListings

中每个条件的商品数量
<CompetitivePricing>
    <CompetitivePrices>
      <CompetitivePrice belongsToRequester="false" condition="New" subcondition="New">
        <CompetitivePriceId>1</CompetitivePriceId>
        <Price>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>18.00</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>18.00</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </Price>
      </CompetitivePrice>
      <CompetitivePrice belongsToRequester="false" condition="Used" subcondition="VeryGood">
        <CompetitivePriceId>2</CompetitivePriceId>
        <Price>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>100.00</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>100.00</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>0.00</Amount>
          </Shipping>
        </Price>
      </CompetitivePrice>
    </CompetitivePrices>
    <NumberOfOfferListings>
      <OfferListingCount condition="New">109</OfferListingCount>
      <OfferListingCount condition="Collectible">1</OfferListingCount>
      <OfferListingCount condition="Used">51</OfferListingCount>
      <OfferListingCount condition="Any">161</OfferListingCount>
    </NumberOfOfferListings>
  </CompetitivePricing>

但是Boto只保留Any值:

    CompetitivePricing{}(
        TradeInValue: None, 
        CompetitivePrices: CompetitivePriceList{}(
            CompetitivePrice: [
                CompetitivePrice{'condition': 'New', 'belongsToRequester': 'false', 'subcondition': 'New'}(
                    CompetitivePriceId: '1', 
                    Price: Price{}(
                        Shipping: USD 0.00, 
                        LandedPrice: USD 18.00, 
                        ListingPrice: USD 18.00
                    )
                ), 
                CompetitivePrice{'condition': 'Used', 'belongsToRequester': 'false', 'subcondition': 'VeryGood'}(
                    CompetitivePriceId: '2', 
                    Price: Price{}(
                        Shipping: USD 0.00, 
                        LandedPrice: USD 100.00, 
                        ListingPrice: USD 100.00
                    )
                )
            ]
        ), 
        NumberOfOfferListings: [''], 
        OfferListingCount: 161{'condition': 'Any'}
    )

请注意,NumberOfOfferListings在解析的响应中包含一个空字符串,并且只保存了XML中的一个OfferListingCount并将其添加为新属性。

有谁知道为什么要删除其他OfferListingCount值,或者对如何保留这些值有一个很好的建议?

我已经搜索并阅读了源代码:https://github.com/boto/boto/blob/develop/boto/mws/response.py#L520并且无法弄清楚它们放弃这些值的位置。我已尝试使用多种产品并获得相同的结果。

编辑:我尝试过使用猴子修补CompetitivePricing

class OfferListingCount(ResponseElement):
    pass


CompetitivePricing.NumberOfOfferListings = Element(OfferListingCount=ElementList(OfferListingCount))

这给了我完整的条件清单:

NumberOfOfferListings: ^NumberOfOfferListings^{}(
    OfferListingCount: [
        OfferListingCount{'condition': 'New'}(), 
        OfferListingCount{'condition': 'Collectible'}(), 
        OfferListingCount{'condition': 'Used'}(), 
        OfferListingCount{'condition': 'Any'}()
    ]
)

但没有价值观。

如果我使用SimpleList

class OfferListingCount(ResponseElement):
    pass


CompetitivePricing.NumberOfOfferListings = Element(OfferListingCount=SimpleList(OfferListingCount))

我得到的是值而不是条件:

NumberOfOfferListings: ^NumberOfOfferListings^{}(
    OfferListingCount: ['109', '1', '54', '164']
)

如此接近

1 个答案:

答案 0 :(得分:2)

这是我最终提出的猴子补丁:

from boto.mws.response import CompetitivePricing, ElementList, ResponseElement, Element

class OfferListingCount(ResponseElement):
    OfferCount = 0

    def endElement(self, name, value, connection):
        self.OfferCount = value
        super(OfferListingCount, self).endElement(name, value, connection)

CompetitivePricing.NumberOfOfferListings = Element(OfferListingCount=ElementList(OfferListingCount))

这给了我想要的输出:

CompetitivePricing{}(
    NumberOfOfferListings: ^NumberOfOfferListings^{}(
        OfferListingCount: [
            OfferListingCount{'condition': 'New'}(OfferCount: '105'), 
            OfferListingCount{'condition': 'Collectible'}(OfferCount: '2'), 
            OfferListingCount{'condition': 'Used'}(OfferCount: '58'), 
            OfferListingCount{'condition': 'Any'}(OfferCount: '165')]
    )
)