基本的に、Shopify の画像 URL がどのように保存されているか、およびそれにアクセスするために何を書くべきかがわかりません。私はPythonの専門家ではないので、私が見ていない単純なものかもしれません。
Shopify には、アプリ開発者がショップから商品や注文などにアクセスできるようにする API があります。ショップのすべての商品のリストを取得して、タイトルや価格などの情報を取得できますが、画像の URL を取得するのに苦労しています。何が起こっているのか、何が必要なのかを示す基本的なコードを次に示します。
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 のリストを作成することです。ProductのShopify API ドキュメントを見ると、product.images
属性が次のようになっていることがわかります。
{ "images" : "[ { "src": "http://example.com/burton.jpg" } ]"}
で pdb.set_trace() を実行するとproduct.images
、次のようになります (プライバシーのために数字を変更しました)。
[image(12345678), image(12345678), image(12345678), image(12345678)]
画像の 1 つで 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