0

You can use this code to get the first image URL of one specific item on amazon:

from amazon.api import AmazonAPI

amazon = AmazonAPI(aws_key='XXX', aws_secret='XXX', aws_associate_tag='XXX', region="DE")
product = amazon.lookup(ItemId='B003P0ZB1K')
print(product.large_image_url)

but how can you get to all image URLs of that item, instead of only getting the first one? Thanks.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
KatharsisHerbie
  • 115
  • 1
  • 10

1 Answers1

2

You need to include the 'Images' response group in your request.

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

The list of XML ImageSets can then be accessed via the images property, but will need to be parsed using an XML parser.

product.images

Please check out this article for information on parsing XML in python: How do I parse XML in Python?

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

From the source code of the library:

@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

The image set XML looks like this:

<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>
zachdb86
  • 971
  • 1
  • 8
  • 13
  • Thanks, tried that too but there is still a problem. Instead of getting image URLs I get some kind of reference?! `[, ]` – KatharsisHerbie Sep 18 '17 at 15:14
  • Looking at the source, there is a property @images which will return a list of all image URLs, you should be able access that with: `product.images` – zachdb86 Sep 18 '17 at 21:14
  • I guess i'm missing something... Thats my code: `product = amazon.lookup(ItemId='B003P0ZB1K', ResponseGroup='Images') print(product.images)`. And it prints me that exact reference text I posted in my last comment... thanks. – KatharsisHerbie Sep 18 '17 at 21:26
  • What version of the amazon.api are you using? Also, the response is a list of Element objects which means the XML response from the Amazon API is not being parsed. – zachdb86 Sep 18 '17 at 21:28
  • Python 3.5.2: python-amazon-simple-product-api 2.2.11 – KatharsisHerbie Sep 19 '17 at 18:26
  • Are you still having issues parsing the XML? – zachdb86 Sep 20 '17 at 19:16