简体   繁体   English

从Python中获取现有Shopify产品图像的源URL

[英]Getting the source URL from an existing Shopify product image in Python

I'm basically having trouble seeing how a Shopify image url is stored and what I should write to access it. 我基本上无法看到如何存储Shopify图像网址以及我应该写什么来访问它。 I'm not a python expert, so it could be something simple I'm not seeing. 我不是蟒蛇专家,所以它可能是我看不到的简单。

Shopify has an API that allows app developers to access products, orders, and the like from a shop. Shopify有一个API,允许应用程序开发人员从商店访问产品,订单等。 I'm able to get a list of all the products of my shop and grab information like the title and price, but I'm having trouble grabbing the image url. 我能够获得我店铺所有产品的清单,并获取标题和价格等信息,但我无法抓住图片网址。 Here's some basic code of what's happening and what I need: 以下是一些基本代码,说明正在发生的事情以及我需要的内容:

import shopify

#other code that accesses my shop

products = shopify.Product.find() #Here I grab a list of all the products
for product in products:
    title = product.title
    price = float(product.variants[0].price) #I'm not sure on the price either, but it works for now
    image_urls = create_image_url_list(product.images) # And here's where I want to create the list of strings using product.images

create_image_url_list(product_images):
    image_urls = []
    for image in product_images:
        product_image_url = '' # Somehow get source url from product image here as string
        image_urls.append(product_image_url)
    return image_urls

I believe each product has a list of image(s). 我相信每个产品都有一个图像列表。 What I want to be able to do is create a list of image source urls as strings for each product. 我希望能够做的是为每个产品创建一个图像源URL列表作为字符串。 Looking at the Shopify API Documentation for Product , I see that the product.images attribute looks like this: 查看产品Shopify API文档 ,我看到product.images属性如下所示:

{ "images" : "[ { "src": "http://example.com/burton.jpg" } ]"}

And when I do a pdb.set_trace() on product.images , it looks like this (I changed the numbers for privacy): 当我在product.images上执行pdb.set_trace()时,它看起来像这样(我更改了隐私号码):

[image(12345678), image(12345678), image(12345678), image(12345678)]

When I do a dir() on one of the images, I get this: 当我在其中一个图像上执行dir()时,我得到了这个:

['_ShopifyResource__get_id', '_ShopifyResource__set_id', '_ShopifyResource__to_xml_element', '__class__', '__cmp__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_build_list', '_build_object', '_class_delete', '_class_get', '_class_head', '_class_post', '_class_put', '_collection_path', '_connection', '_custom_method_collection_url', '_custom_method_element_url', '_custom_method_new_element_url', '_element_path', '_find_class_for', '_find_class_for_collection', '_find_every', '_find_one', '_find_single', '_format', '_headers', '_id_from_response', '_initialized', '_instance_delete', '_instance_get', '_instance_head', '_instance_post', '_instance_put', '_load_attributes_from_response', '_password', '_plural', '_prefix', '_prefix_options', '_prefix_parameters', '_prefix_source', '_primary_key', '_query_string', '_singular', '_site', '_split_options', '_threadlocal', '_timeout', '_update', '_user', 'activate_session', 'attach_image', 'attributes', 'clear_session', 'count', 'create', 'delete', 'destroy', 'errors', 'exists', 'find', 'find_first', 'find_one', 'get', 'get_id', 'head', 'id', 'is_new', 'is_valid', 'klass', 'post', 'put', 'reload', 'save', 'set_id', 'to_dict', 'to_xml']

EDIT: 编辑:

With Pawelmhm's help, the resulting code to accessing the source url is: 有了Pawelmhm的帮助,访问源URL的结果代码是:

products = shopify.Product.find() #Here I grab a list of all the products
for product in products:
    title = product.title
    price = float(product.variants[0].price)
    image_urls = [getattr(i, 'src') for i in product.images] # Reduced it to list comprehension using solution

I think I know what you are looking for here, product_images is a "dictionary-like" object with "images" as keys and a list of dictionaries as values. 我想我知道你在找什么,product_images是一个“类字典”对象,其中“images”作为键,字典列表作为值。 Have you tried something like this? 你尝试过这样的事吗?

for image in product_images:
     #product_image_url = '' 
     image_urls.append(image["images"])

or perhaps? 也许?

for image in product_images.to_dict():
     image_urls.append(image["images"]["src"])

Tell me if it works, and if it doesn't tell me what error are you getting. 告诉我它是否有效,如果它没有告诉我你得到了什么错误。 I'm curious about this. 我对此很好奇。 Also what are you getting when you are doing this your way (the way you have in your code publlished here)? 当你按照自己的方式行事时,你会得到什么(你在这里发布的代码方式)? I mean without this empty string of course which abolishes everything, but without empty string, what list are you getting? 我的意思是没有这个空字符串当然会废除一切,但没有空字符串,你得到什么列表?

EDIT: Ah, this should work! 编辑:啊,这应该工作!

for image in product_images: 
     image_urls.append(image["src"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM