我基本上無法看到Shopify圖像URL如何存儲以及我應該如何寫入才能訪問它。我不是一個Python專家,所以它可能是簡單的,我沒有看到。從Python中的現有Shopify產品圖像獲取源URL
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)]
當我在一個圖像做一個目錄(),我得到這個:
['_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的幫助下,所產生的代碼來訪問源網址是:
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
感謝您的快速回復!所以我嘗試了所有三種變化,並且每個都收到相同的錯誤。 TypeError:'Image'對象沒有屬性'__getitem__' 但是你讓我思考並再次查看dir()信息,我看到'__getattr__'。我查了一下如何使用該方法,並發現getattr(image,'src')工作:)謝謝! – missmely
有沒有辦法在保存後立即獲取圖像的ID?我能想到的唯一方法是從產品字典中獲取圖像列表,並獲取應該是最後添加的最後一張圖像。 – ChrisC