python爬一下shopify爆款产品_从Python中获取现有Shopify产品图像的源URL

我基本上无法看到如何存储Shopify图像网址以及我应该写什么来访问它.我不是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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值