您可以使用以下代码来获取amazon上某一特定商品的第一张图片URL:
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,而不是只获得第一个呢?谢谢。
发布于 2017-09-18 08:19:39
您需要在请求中包含“Images”响应组。
product = amazon.lookup(ItemId='B003P0ZB1K', ResponseGroup='Images')
然后可以通过ImageSets属性访问XML图像列表,但需要使用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>https://stackoverflow.com/questions/46269803
复制相似问题