【问题标题】:Getting the source URL from an existing Shopify product image in Python从 Python 中的现有 Shopify 产品图像中获取源 URL
【发布时间】:2013-07-11 09:22:07
【问题描述】:

我基本上无法查看 Shopify 图片 url 的存储方式以及我应该写什么来访问它。我不是 python 专家,所以这可能是一些我没有看到的简单的东西。

Shopify 有一个 API,允许应用开发者从商店访问产品、订单等。我能够获取我商店所有产品的列表并获取标题和价格等信息,但我无法获取图片网址。这是正在发生的事情和我需要的一些基本代码:

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

我相信每个产品都有一张图片列表。我想要做的是为每个产品创建一个图像源 URL 列表作为字符串。查看Shopify API Documentation for Product,我看到product.images 属性看起来像这样:

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

当我在product.images 上执行 pdb.set_trace() 时,它看起来像这样(为了隐私,我更改了数字):

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

当我对其中一张图片执行 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']

编辑:

在 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

【问题讨论】:

    标签: python image api url shopify


    【解决方案1】:

    我想我知道您在这里寻找什么,product_images 是一个“类字典”对象,其中“图像”作为键,字典列表作为值。你有没有尝试过这样的事情?

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

    或许?

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

    告诉我它是否有效,如果它没有告诉我你遇到了什么错误。我很好奇这个。另外,当您以自己的方式执行此操作时(您在此处发布的代码中的方式),您会得到什么?我的意思是没有这个空字符串当然会废除一切,但是没有空字符串,你会得到什么列表?

    编辑:啊,这应该可行!

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

    【讨论】:

    • 感谢您的快速回复!因此,我尝试了所有三种变体,每种变体都收到了相同的错误。 TypeError: 'Image' object has no attribute 'getitem' 但是你让我思考并再次查看 dir() 信息,我看到了 'getattr'。我查找了如何使用该方法,发现执行 getattr(image, 'src') 有效:) 谢谢!
    • 有没有办法在保存后立即获取图片的id?我能想到的唯一方法是从产品字典中获取图像列表并获取最后一张应该是您添加的最后一张图片。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 2017-09-20
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 2018-06-10
    相关资源
    最近更新 更多