从Amazon API获取所有图像URL

时间:2017-09-17 23:31:25

标签: python api amazon

您可以使用此代码获取亚马逊上某个特定项目的第一张图片网址:

         col1   col2   col3   col4
row1     65      24     47     35
row2     33      48     25     89
row3     65      34     67     34
row4     24      12     52     17

但是如何才能获得该项目的所有图片网址,而不是仅获取第一个?感谢。

1 个答案:

答案 0 :(得分:2)

您需要在请求中包含“图片”响应组。

product = amazon.lookup(ItemId='B003P0ZB1K', ResponseGroup='Images')

然后可以通过images属性访问XML ImageSets列表,但需要使用XML解析器进行解析。

product.images

请查看本文,了解有关在python中解析XML的信息: How do I parse XML in Python?

参考:https://docs.aws.amazon.com/AWSECommerceService/latest/DG/RG_Images.html

从图书馆的源代码:

@property
def images(self):
    """List of images for a response.
    When using lookup with RespnoseGroup 'Images', you'll get a
    list of images. Parse them so they are returned in an easily
    used list format.
    :return:
        A list of `ObjectifiedElement` images
    """
    try:
        images = [image for image in self._safe_get_element(
            'ImageSets.ImageSet')]
    except TypeError:  # No images in this ResponseGroup
        images = []
    return images

图像集XML如下所示:

<ImageSets>
  <ImageSet Category="primary">
  <SwatchImage>
  <URL>https://ecx.images-amazon.com/images/I/51YL4rlI%2B9L._SL30_.jpg</URL>
  <Height Units="pixels">30</Height>
  <Width Units="pixels">23</Width>
  </SwatchImage>
  <SmallImage>
  <URL>https://ecx.images-amazon.com/images/I/51YL4rlI%2B9L._SL75_.jpg</URL>
  <Height Units="pixels">75</Height>
  <Width Units="pixels">58</Width>
  </SmallImage>
  <ThumbnailImage>
  <URL>https://ecx.images-amazon.com/images/I/51YL4rlI%2B9L._SL75_.jpg</URL>
  <Height Units="pixels">75</Height>
  <Width Units="pixels">58</Width>
  </ThumbnailImage>
  <TinyImage>
  <URL>https://ecx.images-amazon.com/images/I/51YL4rlI%2B9L._SL110_.jpg</URL>
  <Height Units="pixels">110</Height>
  <Width Units="pixels">86</Width>
  </TinyImage>
  <MediumImage>
  <URL>https://ecx.images-amazon.com/images/I/51YL4rlI%2B9L._SL160_.jpg</URL>
  <Height Units="pixels">160</Height>
  <Width Units="pixels">124</Width>
  </MediumImage>
  <LargeImage>
  <URL>https://ecx.images-amazon.com/images/I/51YL4rlI%2B9L.jpg</URL>
  <Height Units="pixels">500</Height>
  <Width Units="pixels">389</Width>
  </LargeImage>
 </ImageSet>
</ImageSets>