def update_size_info(spid, sizes, prices): global PSDAO for size in sizes: for price in prices: if size["variantId"] == price["variantId"]: if not price["isInStock"]: PSDAO.set_sku_out_of_stock(spid, size["size"]) print "".join([str(spid), ":", str(size["size"])]) else: psku = ProductSku() psku.spid = spid psku.size = size['size'] curr_price = price['price']['current']['value'] rrp_price = price['price']['rrp']['value'] previous_price = price['price']['previous']['value'] psku.discount_price = curr_price if rrp_price == 0 and previous_price == 0: psku.price = curr_price else: psku.price = rrp_price or previous_price exists_id = PSDAO.get_id_by_spid_size(psku) if exists_id is not None: psku.spider_product_sku_id = exists_id PSDAO.update_product_sku(psku) print "".join([ str(spid), ":", str(psku.spider_product_sku_id), ":", psku.size, ":", str(psku.price) ])
def grab_product(self, flag, url): product = Product() # 来源 product.flag = flag # 源链接 product.url = url # 状态正常 product.status = "1" # 英文 product.language_id = "2" # 品牌 product.brand = "Ted Baker" pg = self.do_visit(url) # 解析商品信息 if self.ana_product_info(product, pg): # 保存商品信息 self.save_product(product) # 保存描述信息 self.save_product_desc(product) pimg = ProductImages() pimg.spid = product.spider_product_id # 解析图片信息 self.ana_product_images(pimg, pg) # 保存图片信息 self.save_product_images(pimg) psku = ProductSku() # 解析并保存sku信息 self.ana_and_save_product_sku(psku, product) else: self.log_info("".join([url, " product not saved!"]))
def grab_product(self, flag, url): product = Product() # 来源 product.flag = flag # 源链接 product.url = url # 状态正常 product.status = "1" # 英文 product.language_id = "2" pg = self.do_visit(url) # 解析商品信息 if self.ana_product_info(product, pg): # 保存商品信息 self.save_product(product) # 保存描述信息 self.save_product_desc(product) # 保存图片信息 pimg = ProductImages() pimg.spid = product.spider_product_id if len(product.images) > 0: pimg.images = ",".join(product.images) self.save_product_images(pimg) else: # 记录没有图片的商品 self.log_info("".join([str(pimg.spid), " no images!"])) psku = ProductSku() # 保存sku信息 self.ana_and_save_product_sku(psku, product) else: self.log_info("".join([url, " product not saved!"]))
def ana_and_save_skus(self, spid, sku_info): # sku 信息在参数 SizesInformationViewModel 的 AvailableSizes中 skus = sku_info["SizesInformationViewModel"]["AvailableSizes"] if len(skus) == 0: # 记录没有尺码的商品 tipstr = "".join([str(spid), " no sku find!"]) print tipstr self.log_info(tipstr) return psku = ProductSku() psku.spid = spid for sku in skus: # 尺码 size_str = sku["Description"] # 尺码所属(国家标准) size_scale = sku["ScaleDescription"] if size_scale is not None: # 以 尺码[标准] 形式保存 size_str = "".join([size_str, "[", size_scale, "]"]) psku.size = size_str # 价格信息 pinfo = sku["PriceInfo"] # 原价 oprice = self.get_price_num(pinfo["FormatedPriceWithoutPromotion"]) # 当前售价 dprice = self.get_price_num(pinfo["FormatedPrice"]) psku.discount_price = dprice if oprice is not None: psku.price = oprice else: psku.price = dprice # 保存 self.save_product_skus(psku)
def grab_product(self, flag, url): # 将连接替换成意大利站点(意大利站是欧元价) surl = url.replace("/cn/", "/it/") pg = self.do_visit(surl) product = Product() # 来源 product.flag = flag # 源链接 product.url = surl # 此站只有女士 product.gender = "1" # 状态正常 product.status = "1" # 中文 product.language_id = "1" # 解析商品信息 if self.ana_product_info(product, pg): # 保存商品信息 self.save_product(product) # 保存商品描述 self.save_product_desc(product) pimg = ProductImages() pimg.spid = product.spider_product_id self.ana_product_images(pimg, pg) self.save_product_images(pimg) psku = ProductSku() psku.spid = product.spider_product_id self.ana_and_save_product_sku(psku, pg) # 语言切换成英文 en_pg = self.do_visit(surl.replace("/zh/", "/en/")) product.language_id = "2" # 保存英文版商品信息 if self.ana_product_info(product, en_pg): self.save_product_desc(product) else: self.log_info("".join([surl, " product not saved!"]))
def save_product_sku(spid, p_variants, price_variants, psdao): psku = ProductSku() psku.spid = spid for provitem in p_variants: p_variant_id = provitem['variantId'] for priceitem in price_variants: price_variant_id = priceitem['variantId'] if p_variant_id == price_variant_id: psku.size = provitem['size'] curr_price = priceitem['price']['current']['value'] rrp_price = priceitem['price']['rrp']['value'] previous_price = priceitem['price']['previous']['value'] psku.discount_price = curr_price if rrp_price == 0 and previous_price == 0: psku.price = curr_price else: psku.price = rrp_price or previous_price exists_id = psdao.get_id_by_spid_size(psku) if exists_id is not None: psku.spider_product_sku_id = exists_id # psdao.update_product_sku(psku) print "".join([str(exists_id), " sku is exists!"]) else: psdao.save(psku)
def grab_product(self, flag, url): product = Product() # 来源 product.flag = flag # 源链接 product.url = url # 状态正常 product.status = "1" # 英语 product.language_id = "2" pg = self.do_visit(url) # 解析商品信息 if self.ana_product_info(product, pg): # 保存商品信息 self.save_product(product) # 保存描述信息 self.save_product_desc(product) pimg = ProductImages() pimg.spid = product.spider_product_id img_arr = [] if len(product.images) > 0: for img in product.images: # -20后缀结束的图片不是需要的图片 if "-20." in img: continue img_arr.append(img) pimg.images = ",".join(img_arr) # 保存图片信息 self.save_product_images(pimg) else: # 记录没有图片的商品 self.log_info("".join([str(pimg.spid), " no images!"])) psku = ProductSku() # 解析并保存sku信息 self.ana_and_save_product_sku(psku, product, pg) else: self.log_info("".join([url, " product not saved!"]))