从Amazon API获取所有图像URL [英] Get all image URLs from Amazon API
问题描述
您可以使用以下代码获取亚马逊上某个特定商品的第一个图片网址:
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)
但是如何获得该项目的所有图像URL,而不是仅获得第一个?谢谢.
but how can you get to all image URLs of that item, instead of only getting the first one? Thanks.
推荐答案
您需要在请求中包括图片"响应组.
You need to include the 'Images' response group in your request.
product = amazon.lookup(ItemId ='B003P0ZB1K',ResponseGroup ='Images')
然后可以通过images属性访问XML ImageSet的列表,但是需要使用XML解析器进行解析.
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
请查看本文,以获取有关在python中解析XML的信息:如何在Python中解析XML?
Please check out this article for information on parsing XML in python: 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看起来像这样:
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>
这篇关于从Amazon API获取所有图像URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!