Getting source URL from existing Shopify product image in Python

I basically have a problem with how the Shopify URL is stored and what I have to write to access it. I'm not a python expert, so it might be something simple that I don't see.

Shopify has an API that allows application developers to access products, orders, etc. from the store. I can get a list of all the products in my store and get information such as name and price, but I’m having problems capturing the image. Here is the basic code of what is 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 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 that each product has a list of images. What I want to do is create a list of URLs of the image source as strings for each product. Looking at Buy API documentation for a product , I see that the product.images attribute is as follows:

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

And when I do pdb.set_trace () on product.images , it looks like this (I changed the numbers for privacy):

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

When I do dir () on one of the images, I get the following:

 ['_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:

Using the Pawelmhm help, the resulting code to access the source URL is:

 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 
+4
source share
1 answer

I think that 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. Have you tried something like this?

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

or maybe?

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

Tell me if this works, and if it does not tell me what error you are getting. I'm interested. And what do you get when you do it your own way (how do you do it in your code posted here)? I mean without this empty line that cancels everything, but without the empty line, what list do you get?

EDIT: Ah, that should work!

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

Source: https://habr.com/ru/post/1491164/


All Articles