提交 2d969077 authored 作者: 梁业锦's avatar 梁业锦 💬

重新排布了爬虫的代码结构

上级 eb49473c
......@@ -2,10 +2,11 @@ package com.diaoyun.zion.chinafrica.bis.impl;
import com.diaoyun.zion.chinafrica.bis.IItemSpider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.ProductResponse;
import com.diaoyun.zion.chinafrica.vo.*;
import com.diaoyun.zion.master.util.HttpClientUtil;
import com.diaoyun.zion.master.util.TranslateHelper;
import com.diaoyun.zion.master.util.spider.UniqloSpiderParse;
import com.diaoyun.zion.master.util.SpiderUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -13,6 +14,7 @@ import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
......@@ -32,7 +34,6 @@ public class UniqloSpider implements IItemSpider {
/**
* Uniqlo 数据爬虫
* @see UniqloSpiderParse#formatProductResponse 格式化数据方法
* @param targetUrl 接收的商品详情路径
* @return 格式化与翻译后的 Json 数据
*/
......@@ -48,11 +49,149 @@ public class UniqloSpider implements IItemSpider {
String priceContent = HttpClientUtil.getContentByUrl(priceUrl, PlatformEnum.UNIQLO.getValue());
JSONObject resultJson = JSONObject.fromObject(content);
JSONObject priceJson = JSONObject.fromObject(priceContent);
ProductResponse productResponse = UniqloSpiderParse.formatProductResponse(resultJson, priceJson, pId);
ProductResponse productResponse = formatProductResponse(resultJson, priceJson, pId);
resultJson = JSONObject.fromObject(productResponse);
// 翻译
TranslateHelper.translateProductResponse(resultJson);
return resultJson;
}
/**
* 返回格式化数据
* @param dataMap 调用优衣库网页接口接收的主要商品数据
* @param pId 商品链接截取的商品id
* @return
*/
private ProductResponse formatProductResponse(JSONObject dataMap, JSONObject priceJson, String pId) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 商品基本信息
ItemInfo itemInfo = new ItemInfo();
// 取 summary 节点对象
JSONObject summaryObj = dataMap.getJSONObject("summary");
//////////////////////////////////// 获取商品基本信息 ////////////////////////////////////////////
itemInfo.setShopName(PlatformEnum.UNIQLO.getLabel());
itemInfo.setShopUrl("https://www.uniqlo.cn/product-detail.html");
itemInfo.setItemId(pId);
itemInfo.setTitle(summaryObj.getString("fullName"));
//////////////////////////////////// 获取商品基本信息End(图片下取) ////////////////////////////////////////////
// 取 rows 节点数组
JSONArray rowsArr = dataMap.getJSONArray("rows");
for (int i = 0; i < rowsArr.size(); i++) {
JSONObject rowsObj = rowsArr.getJSONObject(i);
//////////////////////////////////// 获取商品颜色属性 ////////////////////////////////////////////
Set<ProductProp> propSetColor = new HashSet<>(16);
ProductProp productPropColor = new ProductProp();
// 颜色id
String colorNo = rowsObj.getString("colorNo");
productPropColor.setPropId(colorNo);
// 颜色名
String colorName = rowsObj.getString("style");
productPropColor.setPropName(colorName);
// 颜色图片
String imageUrl = "https://www.uniqlo.cn/hmall/test/" + pId + "/sku/561/" + colorNo + ".jpg";
productPropColor.setImage(imageUrl);
if (i == 0) {
itemInfo.setPic(imageUrl);
}
propSetColor.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSetColor);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSetColor.addAll(oldPropSet);
productPropSet.put("颜色", propSetColor);
}
//////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
///////////////////////// 获取商品尺码属性 ////////////////////
Set<ProductProp> sizePropSetSize = new HashSet<>();
ProductProp productPropSize = new ProductProp();
String size = rowsObj.getString("sizeText");
productPropSize.setPropName(size);
String sizeNo = rowsObj.getString("skuId");
productPropSize.setPropId(sizeNo);
sizePropSetSize.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSetSize);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSetSize.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSetSize);
}
///////////////////////// 获取商品尺码属性 END////////////////////
String skuStr = ";" + colorNo + ";" + sizeNo + ";";
//////////////////////////////////// 获取库存 ////////////////////////////////////////////
// 设置:商品包含库存信息
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
ProductSkuStock productSkuStock = new ProductSkuStock();
// 设置:可用库存值,Uniqlo 未有可用的库存数据
productSkuStock.setSellableQuantity(999);
// 设置:库存对应的id
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
//////////////////////////////////// 获取库存 END/////////////////////////////////////////
//////////////////////////////////// 获取原始价 //////////////////////////////////
OriginalPrice originalPrice = new OriginalPrice();
// 获取商品的原始价
String fullPrice = summaryObj.getString("originPrice");
// TODO 转换汇率,目前商品单位是人民币
fullPrice = SpiderUtil.exchangeRate(fullPrice);
originalPrice.setPrice(fullPrice);
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
//////////////////////////////////// 获取原始价 END//////////////////////////////////
//////////////////////////////////// 获取促销价 //////////////////////////////////
// 取 summary 节点对象
JSONObject summaryPriceObj = priceJson.getJSONArray("resp").getJSONObject(0).getJSONObject("summary");
ProductPromotion productPromotion = new ProductPromotion();
// 获取商品的促销价
String promotionPrice = summaryPriceObj.getString("maxPrice");
// TODO 转换汇率,目前商品单位是人民币
promotionPrice = SpiderUtil.exchangeRate(promotionPrice);
productPromotion.setSkuStr(skuStr);
productPromotion.setPrice(promotionPrice);
productResponse.setPrice(promotionPrice);
productResponse.setSalePrice(promotionPrice + "-" + fullPrice);
promotionList.add(productPromotion);
//////////////////////////////////// 获取促销价 END//////////////////////////////////
}
// 按照一下顺序进行 json 数据的填充
productResponse.setPropFlag(true);
productResponse.setPromotionFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.UNIQLO.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.util.*;
import static com.diaoyun.zion.master.util.spider.SpiderUtil.exchangeRate;
/**
* Adidas 数据爬虫
* @see com.diaoyun.zion.chinafrica.bis.impl.AdidasSpider
* @author 爱酱油不爱醋
*/
public class AdidasSpiderParse {
/**
* 格式化返回数据
* @param content 主要的页面数据
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(String content, String pId) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 解析为 Document 对象
Document document = Jsoup.parse(content);
// 获取商品的价格、颜色、标题、图片、尺码属性
String fullPrice = document.select("input[id=salePrice]").attr("value");
String pColor = document.select("input[id=colorDisPaly]").attr("value");
String pTitle = document.select("input[id=itemTitle]").attr("value");
String pImg = document.select("input[id=shoppingcartpic]").attr("value");
List<String> pSizeList = document.select("div[class=overview product-size]")
.select("ul").select("li").eachText();
//////////////////////////////////// 获取商品基本信息 ////////////////////////////
ItemInfo itemInfo = new ItemInfo();
itemInfo.setShopName(PlatformEnum.ADIDAS.getLabel());
itemInfo.setShopUrl("https://www.adidas.com");
itemInfo.setItemId(pId);
itemInfo.setTitle(pTitle);
itemInfo.setPic(pImg);
//////////////////////////////////// 获取商品基本信息End /////////////////////////
for (int i = 0; i < pSizeList.size(); i++) {
// 库存对应的id(颜色id + 尺码id)
String skuStr = ";" + pId + ";" + pSizeList.get(i) + ";";
///////////////////////// 获取商品尺码属性 ////////////////////
Set<ProductProp> sizePropSet = new HashSet<>();
ProductProp productPropSize = new ProductProp();
productPropSize.setPropId(pSizeList.get(i));
productPropSize.setPropName(pSizeList.get(i));
sizePropSet.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSet.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSet);
}
///////////////////////// 获取商品尺码属性 END////////////////////
//////////////////////////////////// 获取库存 ////////////////////////////////////////////
// 设置:商品包含库存信息
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
ProductSkuStock productSkuStock = new ProductSkuStock();
// 设置:可用库存值,Zara 未有可用的库存数据
productSkuStock.setSellableQuantity(999);
// 设置:库存对应的id
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
//////////////////////////////////// 获取库存 END/////////////////////////////////////////
//////////////////////////////////// 获取原始价 //////////////////////////////////
OriginalPrice originalPrice = new OriginalPrice();
// 获取商品的原始价
// TODO 转换汇率,目前商品单位是人民币
String originalFullPrice = exchangeRate(fullPrice);
originalPrice.setPrice(originalFullPrice);
productResponse.setPrice(originalFullPrice);
productResponse.setSalePrice(originalFullPrice + "-" + originalFullPrice);
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
//////////////////////////////////// 获取原始价 END///////////////////////////////
}
//////////////////////////////////// 获取商品颜色属性 ////////////////////////////
Set<ProductProp> propSet = new HashSet<>(16);
ProductProp productPropColor = new ProductProp();
// 颜色描述
productPropColor.setPropId(pId);
productPropColor.setPropName(pColor);
productPropColor.setImage(pImg);
propSet.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSet.addAll(oldPropSet);
productPropSet.put("颜色", propSet);
}
//////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
// 按照一下顺序进行 json 数据的填充
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.ADIDAS.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.*;
import java.util.regex.Pattern;
import static com.diaoyun.zion.master.util.spider.SpiderUtil.exchangeRate;
/**
* COACH(蔻驰) 爬虫数据解析
* @see com.diaoyun.zion.chinafrica.bis.impl.CoachSpider
* @author 爱酱油不爱醋
*/
public class CoachSpiderParse {
/**
* 格式化返回数据
* @param dataMap 主要的Json数据
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(JSONObject dataMap, String pId) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 取 data 节点对象
JSONObject dataObj = dataMap.getJSONObject("data");
//////////////////////////////////// 获取商品基本信息 ////////////////////////////
ItemInfo itemInfo = new ItemInfo();
itemInfo.setShopName(PlatformEnum.COACH.getLabel());
itemInfo.setShopUrl("https://china.coach.com");
itemInfo.setItemId(pId);
itemInfo.setTitle(dataObj.getString("name"));
//////////////////////////////////// 获取商品基本信息(图片下取)End /////////////////////////
List<String> sizeNoList = new ArrayList<>();
List<String> colorNoList = new ArrayList<>();
// 取 attributes 节点数组
JSONArray attributesArr = dataObj.getJSONArray("attributes");
for (int i = 0; i < attributesArr.size(); i++) {
///////////////////////// 获取商品颜色属性 ////////////////////////////////////////////////////////////////
// 0 位为颜色属性
if (i == 0) {
// 取 values 节点数组
JSONArray valuesArr = attributesArr.getJSONObject(i).getJSONArray("values");
Set<ProductProp> propSet = new HashSet<>(16);
for (int j = 0; j < valuesArr.size(); j++) {
JSONObject valuesObj = valuesArr.getJSONObject(j);
// 获取图片路径
String imageUrl = valuesObj.getString("image");
// 设置商品基本信息的图片
if (i == 0) {
itemInfo.setPic(imageUrl);
}
ProductProp productPropColor = new ProductProp();
String colorNo = valuesObj.getString("value_index");
colorNoList.add(colorNo);
productPropColor.setPropId(colorNo);
productPropColor.setPropName(valuesObj.getString("label"));
productPropColor.setImage(imageUrl);
propSet.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSet.addAll(oldPropSet);
productPropSet.put("颜色", propSet);
}
}
///////////////////////// 获取商品颜色属性End ////////////////////////////////////////////////////////////////
///////////////////////// 获取商品尺码属性 ////////////////////////////////////////////////////////////////
// 1 位为尺寸属性(有的商品不一定会存在,如手提包)
} else if (i == 1) {
// 取 values 节点数组
JSONArray valuesArr = attributesArr.getJSONObject(i).getJSONArray("values");
Set<ProductProp> sizePropSet = new HashSet<>();
for (int j = 0; j < valuesArr.size(); j++) {
JSONObject valuesObj = valuesArr.getJSONObject(j);
ProductProp productPropSize = new ProductProp();
String sizeNo = valuesObj.getString("value_index");
productPropSize.setPropId(sizeNo);
sizeNoList.add(sizeNo);
productPropSize.setPropName(valuesObj.getString("label"));
sizePropSet.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSet.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSet);
}
///////////////////////// 获取商品尺码属性 END/////////////////////////////////////////////////////
}
}
}
for (String colorNo : colorNoList) {
for (String sizeNo : sizeNoList) {
// 设置 skuStr
String skuStr = ";" + colorNo + ";" + sizeNo + ";";
// 设置:商品包含库存信息
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
ProductSkuStock productSkuStock = new ProductSkuStock();
// 设置:可用库存值,未有可用的库存数据
productSkuStock.setSellableQuantity(999);
// 设置:库存对应的id
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
//////////////////////////////////// 获取库存 END/////////////////////////////////////////
//////////////////////////////////// 获取原始价 //////////////////////////////////
// 获取商品的原始价(存在优惠价格)
OriginalPrice originalPrice = new OriginalPrice();
String fullPrice = Pattern.compile("[^0-9]").matcher(dataObj.getString("price")).replaceAll("").trim();
// TODO 转换汇率,目前商品单位是人民币
fullPrice = exchangeRate(fullPrice);
productResponse.setPrice(fullPrice);
productResponse.setSalePrice(fullPrice + "-" + fullPrice);
originalPrice.setPrice(fullPrice);
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
//////////////////////////////////// 获取原始价 END //////////////////////////////////
}
}
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.COACH.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.*;
import static com.diaoyun.zion.master.util.spider.SpiderUtil.exchangeRate;
/**
* Esprit 数据爬虫
* @see com.diaoyun.zion.chinafrica.bis.impl.EspritSpider
* @author 爱酱油不爱醋
*/
public class EspritSpiderParse {
/**
* 格式化返回数据
* @param dataMap 主要的Json数据
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(JSONObject dataMap) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 取 product 下的 details 节点对象
JSONObject detailsObj = dataMap.getJSONObject("product").getJSONObject("details");
//////////////////////////////////// 获取商品基本信息 ////////////////////////////
ItemInfo itemInfo = new ItemInfo();
itemInfo.setShopName(PlatformEnum.ESPRIT.getLabel());
itemInfo.setShopUrl("https://www.esprit.cn");
itemInfo.setItemId(detailsObj.getString("code"));
itemInfo.setTitle(detailsObj.getString("title"));
//////////////////////////////////// 获取商品基本信息(图片下取)End /////////////////////////
//////////////////////////////////// 获取商品颜色属性 ////////////////////////////////////////////
// 取 options 的0位的 value 节点数组
JSONArray values_0_Arr = detailsObj.getJSONArray("options").getJSONObject(0).getJSONArray("values");
List<String> colorNoList = new ArrayList<>();
Set<ProductProp> propSet = new HashSet<>(16);
for (int i = 0; i < values_0_Arr.size(); i++) {
JSONObject values_0_Obj = values_0_Arr.getJSONObject(i);
// 获取图片路径
String imageUrl = values_0_Obj.getJSONArray("images").getJSONObject(0).getString("url");
// 设置商品基本信息的图片
if (i == 0) {
itemInfo.setPic(imageUrl);
}
ProductProp productPropColor = new ProductProp();
String colorNo = values_0_Obj.getString("code");
colorNoList.add(colorNo);
productPropColor.setPropId(colorNo);
productPropColor.setPropName(values_0_Obj.getString("displayName"));
productPropColor.setImage(imageUrl);
propSet.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSet.addAll(oldPropSet);
productPropSet.put("颜色", propSet);
}
}
//////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
///////////////////////// 获取商品尺码属性 ////////////////////////////////////////////////////////////////
// 取 options 的 1 位的 value 节点数组
List<String> sizeNoList = new ArrayList<>();
Set<ProductProp> sizePropSet = new HashSet<>();
JSONArray values_1_Arr = detailsObj.getJSONArray("options").getJSONObject(1).getJSONArray("values");
for (int i = 0; i < values_1_Arr.size(); i++) {
JSONObject values_1_Obj = values_1_Arr.getJSONObject(i);
ProductProp productPropSize = new ProductProp();
String sizeNo = values_1_Obj.getString("code");
productPropSize.setPropId(sizeNo);
sizeNoList.add(sizeNo);
productPropSize.setPropName(values_1_Obj.getString("displayName"));
sizePropSet.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSet.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSet);
}
///////////////////////// 获取商品尺码属性 END/////////////////////////////////////////////////////
}
for (String colorNo : colorNoList) {
for (String sizeNo : sizeNoList) {
// 设置 skuStr
String skuStr = ";" + colorNo + ";" + sizeNo + ";";
//////////////////////////////////// 获取库存 ////////////////////////////////////////////
// 设置:商品包含库存信息
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
ProductSkuStock productSkuStock = new ProductSkuStock();
// 设置:可用库存值,Zara 未有可用的库存数据
productSkuStock.setSellableQuantity(999);
// 设置:库存对应的id
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
//////////////////////////////////// 获取库存 END/////////////////////////////////////////
//////////////////////////////////// 获取原始价 //////////////////////////////////
OriginalPrice originalPrice = new OriginalPrice();
// 获取商品的原始价
String fullPrice = detailsObj.getJSONObject("salePrice").getString("amount");
// TODO 转换汇率,目前商品单位是人民币
fullPrice = exchangeRate(fullPrice);
originalPrice.setPrice(fullPrice);
productResponse.setPrice(fullPrice);
productResponse.setSalePrice(fullPrice + "-" + fullPrice);
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
//////////////////////////////////// 获取原始价 END//////////////////////////////////
}
}
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.ESPRIT.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import com.diaoyun.zion.master.util.JsoupUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.math.BigDecimal;
import java.util.*;
import static com.diaoyun.zion.master.util.spider.SpiderUtil.exchangeRate;
/**
* H&M 爬虫数据解析
* @see com.diaoyun.zion.chinafrica.bis.impl.HmSpider
* @author 爱酱油不爱醋
*/
public class HMSpiderParse {
/**
* 格式化返回数据
* @param content 页面数据
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(String content) {
// targetUrl=https://m2.hm.com/m/zh_cn/productpage.0806412004.html
// 获取主要数据并将转换 Json 数据及 Document 对象
String detailStr = JsoupUtil.getScriptContent(content, "productArticleDetails");
int firstBrackets = detailStr.indexOf("{");
int lastbrackets = detailStr.lastIndexOf("}");
String resultStr = detailStr.substring(firstBrackets,lastbrackets+1);
resultStr = resultStr.replaceAll("\'", "\"")
.replaceAll("\"image\": isDesktop [?] ", "")
.replaceAll("\"fullscreen\": isDesktop [?] ", "")
.replaceAll("\"zoom\": isDesktop [?] ", "");
JSONObject dataMap = JSONObject.fromObject(resultStr);
Document document = Jsoup.parse(content);
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
//////////////////////////////////// 获取商品基本信息 ////////////////////////////
ItemInfo itemInfo = new ItemInfo();
itemInfo.setShopName(PlatformEnum.HM.getLabel());
itemInfo.setShopUrl("https://www2.hm.com/");
itemInfo.setItemId(document.select("div[class=article-code]").select("li").text());
itemInfo.setTitle(document.select("h1[class=primary product-item-headline]").text());
//////////////////////////////////// 获取商品基本信息(图片下取)End /////////////////////////
//////////////////////////////////// 获取商品颜色属性 ////////////////////////////////////////////
// 取页面的数据
Elements colorEle = document.select("div[class=mini-slider]").select("ul[class=inputlist clearfix]").select("li");
Map<String, String> colorMap = new HashMap<>(16);
Set<ProductProp> propSet = new HashSet<>(16);
for (Element element : colorEle) {
ProductProp productPropColor = new ProductProp();
String color = element.select("a").attr("data-color");
String colorNo = element.select("a").attr("data-articlecode");
String imgUrl = element.select("noscript").attr("data-src");
colorMap.put(colorNo, color);
itemInfo.setPic(imgUrl);
productPropColor.setPropId(colorNo);
productPropColor.setPropName(color);
productPropColor.setImage(imgUrl);
propSet.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSet.addAll(oldPropSet);
productPropSet.put("颜色", propSet);
}
}
//////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
///////////////////////// 获取商品尺码属性 ///////////////////////////////////////////////////////////
Map<String, String> sizeMap = new HashMap<>(16);
Map<String, Map<String, String>> colorHaveSizeMap = new HashMap<>(16);
Set<ProductProp> sizePropSet = new HashSet<>();
for (Map.Entry<String, String> entry : colorMap.entrySet()) {
JSONArray sizeArr = dataMap.getJSONObject(entry.getKey()).getJSONArray("sizes");
for (int i = 0; i < sizeArr.size(); i++) {
JSONObject sizeObj = sizeArr.getJSONObject(i);
ProductProp productPropSize = new ProductProp();
String sizeNo = sizeObj.getString("sizeCode");
String size = sizeObj.getString("name");
sizeMap.put(sizeNo, size);
productPropSize.setPropId(sizeNo);
productPropSize.setPropName(size);
sizePropSet.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSet.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSet);
}
}
colorHaveSizeMap.put(entry.getKey(), sizeMap);
}
///////////////////////// 获取商品尺码属性 END/////////////////////////////////////////////////////
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
// 获取价格
String fullPrice = document.select("div[class=primary-row product-item-price]").text();
fullPrice = SpiderUtil.retainNumber(fullPrice);
// TODO 转换汇率,目前商品单位是人民币
fullPrice = exchangeRate(fullPrice);
BigDecimal priceOld = new BigDecimal(fullPrice);
BigDecimal div = new BigDecimal("100");
fullPrice = priceOld.divide(div, 2, BigDecimal.ROUND_DOWN).toString();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
for (Map.Entry<String, Map<String, String>> colorEntry : colorHaveSizeMap.entrySet()) {
for (Map.Entry<String, String> sizeEntry : sizeMap.entrySet()) {
// 设置 skuStr
String skuStr = ";" + colorEntry.getKey() + ";" + sizeEntry.getKey() + ";";
//////////////////////////////////// 获取库存 ////////////////////////////////////////////
// H&M 尚未可用的库存
ProductSkuStock productSkuStock = new ProductSkuStock();
productSkuStock.setSkuStr(skuStr);
productSkuStock.setSellableQuantity(999);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
//////////////////////////////////// 获取库存 END/////////////////////////////////////////
//////////////////////////////////// 获取原始价 //////////////////////////////////
OriginalPrice originalPrice = new OriginalPrice();
originalPrice.setSkuStr(skuStr);
originalPrice.setPrice(fullPrice);
originalPriceList.add(originalPrice);
productResponse.setPrice(fullPrice);
productResponse.setSalePrice(fullPrice + "-" + fullPrice);
//////////////////////////////////// 获取原始价 END//////////////////////////////////
}
}
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.HM.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.*;
import static com.diaoyun.zion.master.util.spider.SpiderUtil.exchangeRate;
/**
* Levi(李维斯) 爬虫数据解析
* @see com.diaoyun.zion.chinafrica.bis.impl.LeviSpider
* @author 爱酱油不爱醋
*/
public class LeviSpiderParse {
/**
* 格式化返回数据
* @param dataMap 主要的Json数据
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(JSONObject dataMap) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 取 product 下的 details 节点对象
JSONObject detailsObj = dataMap.getJSONObject("product").getJSONObject("details");
//////////////////////////////////// 获取商品基本信息 ////////////////////////////
ItemInfo itemInfo = new ItemInfo();
itemInfo.setShopName(PlatformEnum.LEVI.getLabel());
itemInfo.setShopUrl("https://www.levi.com");
itemInfo.setItemId(detailsObj.getString("code"));
itemInfo.setTitle(detailsObj.getString("title"));
//////////////////////////////////// 获取商品基本信息(图片下取)End /////////////////////////
//////////////////////////////////// 获取商品颜色属性 ////////////////////////////////////////////
// 取 options 的0位的 value 节点数组
JSONArray values_0_Arr = detailsObj.getJSONArray("options").getJSONObject(0).getJSONArray("values");
List<String> colorNoList = new ArrayList<>();
Set<ProductProp> propSet = new HashSet<>(16);
for (int i = 0; i < values_0_Arr.size(); i++) {
JSONObject values_0_Obj = values_0_Arr.getJSONObject(i);
// 获取图片路径
String imageUrl = values_0_Obj.getJSONArray("images").getJSONObject(0).getString("url");
// 设置商品基本信息的图片
if (i == 0) {
itemInfo.setPic(imageUrl);
}
ProductProp productPropColor = new ProductProp();
String colorNo = values_0_Obj.getString("code");
colorNoList.add(colorNo);
productPropColor.setPropId(colorNo);
productPropColor.setPropName(values_0_Obj.getString("displayName"));
productPropColor.setImage(imageUrl);
propSet.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSet.addAll(oldPropSet);
productPropSet.put("颜色", propSet);
}
}
//////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
///////////////////////// 获取商品尺码属性 ////////////////////////////////////////////////////////////////
// 取 options 的 1 位的 value 节点数组
List<String> sizeNoList = new ArrayList<>();
Set<ProductProp> sizePropSet = new HashSet<>();
JSONArray values_1_Arr = detailsObj.getJSONArray("options").getJSONObject(1).getJSONArray("values");
for (int i = 0; i < values_1_Arr.size(); i++) {
JSONObject values_1_Obj = values_1_Arr.getJSONObject(i);
ProductProp productPropSize = new ProductProp();
String sizeNo = values_1_Obj.getString("code");
productPropSize.setPropId(sizeNo);
sizeNoList.add(sizeNo);
productPropSize.setPropName(values_1_Obj.getString("displayName"));
sizePropSet.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSet.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSet);
}
}
///////////////////////// 获取商品尺码属性 END/////////////////////////////////////////////////////
for (String colorNo : colorNoList) {
for (String sizeNo : sizeNoList) {
// 设置 skuStr
String skuStr = ";" + colorNo + ";" + sizeNo + ";";
//////////////////////////////////// 获取库存 ////////////////////////////////////////////
// 设置:商品包含库存信息
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
ProductSkuStock productSkuStock = new ProductSkuStock();
// 设置:可用库存值,Zara 未有可用的库存数据
productSkuStock.setSellableQuantity(999);
// 设置:库存对应的id
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
//////////////////////////////////// 获取库存 END/////////////////////////////////////////
//////////////////////////////////// 获取原始价 //////////////////////////////////
OriginalPrice originalPrice = new OriginalPrice();
// 获取商品的原始价
String fullPrice = detailsObj.getJSONObject("salePrice").getString("amount");
// TODO 转换汇率,目前商品单位是人民币
fullPrice = exchangeRate(fullPrice);
originalPrice.setPrice(fullPrice);
productResponse.setPrice(fullPrice);
productResponse.setSalePrice(fullPrice + "-" + fullPrice);
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
//////////////////////////////////// 获取原始价 END//////////////////////////////////
}
}
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.LEVI.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.util.*;
import static com.diaoyun.zion.master.util.spider.SpiderUtil.exchangeRate;
/**
* Lily 爬虫数据解析
* @see com.diaoyun.zion.chinafrica.bis.impl.LilySpider
* @author 爱酱油不爱醋
*/
public class LilySpiderParse {
/**
* 格式化返回数据
* TODO 未完成
* @param content 主要的商品内容
* @param pId 商品id
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(String content, String pId) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 解析为 Document 对象
Document document = Jsoup.parse(content);
// 获取商品的价格、颜色、标题、图片、尺码属性
String fullPrice = document.select("input[id=salePrice]").attr("value");
String pColor = document.select("input[id=colorDisPaly]").attr("value");
String pTitle = document.select("input[id=itemTitle]").attr("value");
String pImg = document.select("input[id=shoppingcartpic]").attr("value");
List<String> pSizeList = document.select("div[class=overview product-size]")
.select("ul").select("li").eachText();
//////////////////////////////////// 获取商品基本信息 ////////////////////////////
ItemInfo itemInfo = new ItemInfo();
// 按照一下顺序进行 json 数据的填充
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.ZARA.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.math.BigDecimal;
import java.util.*;
/**
* MassimoDutti 爬虫数据解析
* @see com.diaoyun.zion.chinafrica.bis.impl.MassimoduttiSpider 数据爬虫
* @author 爱酱油不爱醋
*/
public class MassimoDuttiSpiderParse {
/**
* 格式化返回数据
* @param dataMap 主要的 json 数据
* @param pId 商品链接的 id
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(JSONObject dataMap, String pId) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 商品基本信息
ItemInfo itemInfo = new ItemInfo();
//////////////////////////////////// 获取商品基本信息 ////////////////////////////////////////////
itemInfo.setShopName(PlatformEnum.MASSIMODUTTI.getLabel());
itemInfo.setShopUrl("https://www.massimodutti.cn/cn/");
itemInfo.setItemId(pId);
itemInfo.setTitle(dataMap.getString("name"));
//////////////////////////////////// 获取商品基本信息End(图片下取) ////////////////////////////////////////////
// 取 detail 节点对象
JSONObject detailObj = dataMap.getJSONObject("detail");
// 取 colors 节点数组
JSONArray colorsArr = detailObj.getJSONArray("colors");
for (int i = 0; i < colorsArr.size(); i++) {
JSONObject colorsObj = colorsArr.getJSONObject(i);
// 取 image 节点对象
JSONObject imageObj = colorsObj.getJSONObject("image");
// 处理商品链接
String imageUrl = "https://static.massimodutti.cn/3/photos"
+ imageObj.getString("url")
+ "_2_5_16.jpg?t="
+ imageObj.getString("timestamp");
if (i == 0) {
itemInfo.setPic(imageUrl);
}
//////////////////////////////////// 获取商品颜色属性 ////////////////////////////////////////////
Set<ProductProp> propSetColor = new HashSet<>(16);
ProductProp productPropColor = new ProductProp();
// 颜色id
String colorNo = colorsObj.getString("id");
productPropColor.setPropId(colorNo);
// 颜色名
productPropColor.setPropName(colorsObj.getString("name"));
// 颜色图片
productPropColor.setImage(imageUrl);
if (i == 0) {
itemInfo.setPic(imageUrl);
}
propSetColor.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSetColor);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSetColor.addAll(oldPropSet);
productPropSet.put("颜色", propSetColor);
}
//////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
///////////////////////// 获取商品尺码属性 ////////////////////
// 取 sizes 节点对象
JSONArray sizesArr = colorsObj.getJSONArray("sizes");
for (int j = 0; j < sizesArr.size(); j++) {
JSONObject sizesObj = sizesArr.getJSONObject(j);
Set<ProductProp> sizePropSetSize = new HashSet<>(16);
ProductProp productPropSize = new ProductProp();
productPropSize.setPropName(sizesObj.getString("name"));
String sizeNo = sizesObj.getString("sku");
productPropSize.setPropId(sizeNo);
sizePropSetSize.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSetSize);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSetSize.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSetSize);
}
///////////////////////// 获取商品尺码属性 END////////////////////
// 库存 id
String skuStr = ";" + colorNo + ";" + sizeNo;
//////////////////////////////////// 获取库存 ////////////////////////////////////////////
// 设置:商品包含库存信息
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
ProductSkuStock productSkuStock = new ProductSkuStock();
// 设置:可用库存值,Uniqlo 未有可用的库存数据
productSkuStock.setSellableQuantity(999);
// 设置:库存对应的id
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
//////////////////////////////////// 获取库存 END/////////////////////////////////////////
//////////////////////////////////// 获取原始价 //////////////////////////////////
OriginalPrice originalPrice = new OriginalPrice();
// 获取商品的原始价
String fullPrice = sizesObj.getString("price");
BigDecimal priceOld = new BigDecimal(fullPrice);
BigDecimal div = new BigDecimal("100");
BigDecimal priceNew = priceOld.divide(div, 2, BigDecimal.ROUND_HALF_UP);
// TODO 转换汇率,目前商品单位是人民币
fullPrice = SpiderUtil.exchangeRate(priceNew.toString());
originalPrice.setPrice(fullPrice);
productResponse.setPrice(fullPrice);
productResponse.setSalePrice(fullPrice + "-" + fullPrice);
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
//////////////////////////////////// 获取原始价 END//////////////////////////////////
}
}
// 按照一下顺序进行 json 数据的填充
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.MASSIMODUTTI.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.*;
import static com.diaoyun.zion.master.util.spider.SpiderUtil.exchangeRate;
/**
* MO&Co. 爬虫数据解析
* @see com.diaoyun.zion.chinafrica.bis.impl.MocoSpider
* @author 爱酱油不爱醋
*/
public class MocoSpiderParse {
/**
* 格式化返回数据
* @param dataMap 主要的 Json 内容
* @param pId 截取的商品 id
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(JSONObject dataMap, String pId) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 取 productData 对象节点
JSONObject productDataObj = dataMap.getJSONObject("productData");
//////////////////////////////////// 获取商品基本信息 ////////////////////////////////////////////
ItemInfo itemInfo = new ItemInfo();
itemInfo.setShopName("MO&Co.");
itemInfo.setShopUrl("https://en.mo-co.com/");
itemInfo.setItemId(pId);
itemInfo.setTitle(productDataObj.getString("name"));
//////////////////////////////////// 获取商品基本信息End(图片下取) ////////////////////////////////////////////
//////////////////////////////////// 获取商品颜色属性 ////////////////////////////////////////////
// 取 baseOptions 对象数组的 1 位的 options 节点数组
JSONArray options_1_Arr = productDataObj.getJSONArray("baseOptions").getJSONObject(1).getJSONArray("options");
List<String> colorNoList = new ArrayList<>();
Set<ProductProp> propSet = new HashSet<>(16);
for (int i = 0; i < options_1_Arr.size(); i++) {
JSONObject options_1_Obj = options_1_Arr.getJSONObject(i);
// 获取图片的路径
String[] spiltImg = options_1_Obj.getJSONArray("variantOptionQualifiers")
.getJSONObject(0).getJSONObject("image").getString("url").split("_other_");
String imageUrl = "https://mallimg.moco.com/" + pId + "_list_" + spiltImg[1];
// 设置商品基本信息的图片
if (i == 0) {
itemInfo.setPic(imageUrl);
}
ProductProp productPropColor = new ProductProp();
String colorNo = options_1_Obj.getString("epoColorCode");
colorNoList.add(colorNo);
productPropColor.setPropId(colorNo);
productPropColor.setPropName(options_1_Obj.getString("epoColorName"));
productPropColor.setImage(imageUrl);
propSet.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSet.addAll(oldPropSet);
productPropSet.put("颜色", propSet);
}
}
//////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
///////////////////////// 获取商品尺码属性 ////////////////////////////////////////////////////////////////
// 取 baseOptions 的 0 位的 options 节点数组
List<String> sizeNoList = new ArrayList<>();
Set<ProductProp> sizePropSet = new HashSet<>();
JSONArray options_0_Arr = productDataObj.getJSONArray("baseOptions").getJSONObject(0).getJSONArray("options");
for (int i = 0; i < options_0_Arr.size(); i++) {
JSONObject options_0_Obj = options_0_Arr.getJSONObject(i);
ProductProp productPropSize = new ProductProp();
String sizeNo = options_0_Obj.getString("epoSizeCode");
productPropSize.setPropId(sizeNo);
sizeNoList.add(sizeNo);
productPropSize.setPropName(options_0_Obj.getString("epoSizeName") +options_0_Obj.getString("sizeDescription"));
sizePropSet.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSet.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSet);
}
///////////////////////// 获取商品尺码属性 END/////////////////////////////////////////////////////
}
for (String colorNo : colorNoList) {
for (String sizeNo : sizeNoList) {
// 设置 skuStr
String skuStr = ";" + colorNo + ";" + sizeNo + ";";
//////////////////////////////////// 获取库存 ////////////////////////////////////////////
// 设置:商品包含库存信息
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
ProductSkuStock productSkuStock = new ProductSkuStock();
// 设置:可用库存值,Zara 未有可用的库存数据
productSkuStock.setSellableQuantity(999);
// 设置:库存对应的id
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
//////////////////////////////////// 获取库存 END/////////////////////////////////////////
//////////////////////////////////// 获取原始价 //////////////////////////////////
OriginalPrice originalPrice = new OriginalPrice();
// 获取商品的原始价
String fullPrice = productDataObj.getJSONObject("price").getString("value");
// TODO 转换汇率,目前商品单位是人民币
fullPrice = exchangeRate(fullPrice);
originalPrice.setPrice(fullPrice);
productResponse.setPrice(fullPrice);
productResponse.setSalePrice(fullPrice + "-" + fullPrice);
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
//////////////////////////////////// 获取原始价 END//////////////////////////////////
}
}
// 按照一下顺序进行 json 数据的填充
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.MOCO.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.*;
/**
* Ochirly 数据爬虫
* @see com.diaoyun.zion.chinafrica.bis.impl.OchirlySpider
* @author 爱酱油不爱醋
*/
public class OchirlySpiderParse {
/**
* 格式化返回数据
* @param content 主要的页面数据
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(String content) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
Document document = Jsoup.parse(content);
// 获取标题
Elements detailEle = document.select("div[class=detail]").select("div[class=desc]");
String pTitle = detailEle.select("h5").text();
// 获取价格
Elements priceEle = detailEle.select("p[class=price]");
String fullPrice = priceEle.attr("data-list-price");
// 获取颜色id与图片
Elements colorEle = document.select("div[class=color]").select("ul[class=clearfix]");
List<String> imgUrlList = colorEle.select("a").eachAttr("href");
List<String> pColorNoList = new ArrayList<>();
for (int i = 0; i < imgUrlList.size(); i++) {
String hrefStr = imgUrlList.get(i);
if (hrefStr.contains("/p/mobile/")) {
String[] spilt = hrefStr.split("/mobile/");
pColorNoList.add(spilt[1].replaceAll(".shtml", ""));
} else {
pColorNoList.add(0, priceEle.attr("data-sku"));
}
}
List<String> pColorList = new ArrayList<>();
pColorList.addAll(pColorNoList);
List<String> pImgList = colorEle.select("img").eachAttr("src");
// 获取尺码
Elements sizeEle = document.select("div[class=size]").select("div[class=size_contain]").select("li");
List<String> pSizeList = new ArrayList<>();
List<String> pSizeNoList = new ArrayList<>();
for (Element element : sizeEle) {
if (element.hasAttr("data-size-id")) {
pSizeList.add(element.text());
pSizeNoList.add(element.attr("data-size-id"));
}
}
//////////////////////////////////// 获取商品基本信息 ////////////////////////////
ItemInfo itemInfo = new ItemInfo();
itemInfo.setShopName(PlatformEnum.OCHIRLY.getLabel());
itemInfo.setShopUrl("www.ochirly.com");
itemInfo.setItemId(detailEle.select("p[class=price]").attr("data-sku"));
itemInfo.setTitle(pTitle);
itemInfo.setPic(pImgList.get(0));
//////////////////////////////////// 获取商品基本信息End /////////////////////////
//////////////////////////////////// 获取商品颜色属性 ////////////////////////////
for (int i = 0; i < pColorList.size(); i++) {
Set<ProductProp> propSet = new HashSet<>(16);
ProductProp productPropColor = new ProductProp();
productPropColor.setPropName(pColorList.get(i));
productPropColor.setPropId(pColorNoList.get(i));
productPropColor.setImage(pImgList.get(i));
propSet.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSet.addAll(oldPropSet);
productPropSet.put("颜色", propSet);
}
}
//////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
///////////////////////// 获取商品尺码属性 ////////////////////
for (int i = 0; i < pSizeList.size(); i++) {
Set<ProductProp> sizePropSet = new HashSet<>();
ProductProp productPropSize = new ProductProp();
productPropSize.setPropId(pSizeNoList.get(i));
productPropSize.setPropName(pSizeList.get(i));
sizePropSet.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSet.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSet);
}
}
///////////////////////// 获取商品尺码属性 END////////////////////
//////////////////////////////////// 获取库存与原始价 ////////////////////////////////////////////
for (String pColorNo : pColorNoList) {
for (String pSizeNo : pSizeNoList) {
// 设置库存id
String skuStr = ";" + pColorNo + ";" + pSizeNo + ";";
// 设置:商品包含库存信息
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
ProductSkuStock productSkuStock = new ProductSkuStock();
OriginalPrice originalPrice = new OriginalPrice();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
// 设置:可用库存值,未有可用的库存数据
productSkuStock.setSellableQuantity(999);
// 设置:库存对应的id
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
// TODO 转换汇率,目前商品单位是人民币
String originalFullPrice = SpiderUtil.exchangeRate(fullPrice);
originalPrice.setPrice(originalFullPrice);
productResponse.setPrice(originalFullPrice);
productResponse.setSalePrice(originalFullPrice + "-" + originalFullPrice);
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
}
}
//////////////////////////////////// 获取库存与原始价 END///////////////////////////////
// 按照一下顺序进行 json 数据的填充
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.OCHIRLY.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.constant.KeyConstant;
import com.diaoyun.zion.chinafrica.entity.TbCfFeeEntity;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.service.TbCfFeeService;
import com.diaoyun.zion.chinafrica.vo.*;
import com.diaoyun.zion.master.util.SpringContextUtil;
import com.stripe.model.Product;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Pattern;
public class SpiderUtil {
private static BigDecimal rate;
static {
TbCfFeeService tbCfFeeService = (TbCfFeeService) SpringContextUtil.getBean("tbCfFeeService");
rate = tbCfFeeService.getRateFee().getFeeRate();
}
/**
* 转换汇率
*
* @param fullPrice
* @return
*/
protected static String exchangeRate(String fullPrice) {
/* 获取人民币汇率 1美元换取人民币
* TODO 同步汇率问题
* 暂时从后台获取人工设置的汇率
*/
return new BigDecimal(fullPrice).divide(rate, 2, BigDecimal.ROUND_UP).toString();
}
/**
* 去除除了数字之外的所有字符
*
* @param str 字符串
* @return 只有数字的字符串
*/
public static String retainNumber(String str) {
str = Pattern.compile("[^0-9]").matcher(str).replaceAll("").trim();
return str;
}
/**
* 格式化 gap 返回数据
*
* @param dataMap
* @return
*/
public static ProductResponse formatGapProductResponse(JSONObject dataMap) {
ProductResponse productResponse = new ProductResponse();
//原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
//促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
Map<String, Set<ProductProp>> productPropSet = new HashMap<>();
JSONArray productList = dataMap.getJSONArray("productList");
//商品信息
ItemInfo itemInfo = new ItemInfo();
for (int index = 0; index < productList.size(); index++) {
JSONObject propObj = productList.getJSONObject(index);
//////////////////获取价格//////////////////
JSONArray skuList = propObj.getJSONArray("skuList");
for (int i = 0; i < skuList.size(); i++) {
JSONObject skuValue = skuList.getJSONObject(i);
JSONArray attrSaleList = skuValue.getJSONArray("attrSaleList");
String skuStr = ";";
for (int m = 0; m < attrSaleList.size(); m++) {
JSONObject attrSale = attrSaleList.getJSONObject(m);
JSONArray attributeValueList = attrSale.getJSONArray("attributeValueList");
skuStr = skuStr + attributeValueList.getJSONObject(0).getString("code") + ";";
}
//原始价格
OriginalPrice originalPrice = new OriginalPrice();
originalPrice.setSkuStr(skuStr);
String listPrice = skuValue.getString("listPrice");
//转换汇率
listPrice = exchangeRate(listPrice);
originalPrice.setPrice(listPrice);
originalPriceList.add(originalPrice);
//促销价格
if (StringUtils.isNotBlank(skuValue.getString("salePrice"))) {
String salePrice = skuValue.getString("salePrice");
//转换汇率
salePrice = exchangeRate(salePrice);
productResponse.setPromotionFlag(true);
ProductPromotion productPromotion = new ProductPromotion();
productPromotion.setSkuStr(skuStr);
productPromotion.setPrice(salePrice);
promotionList.add(productPromotion);
}
}
//////////////////获取价格 END//////////////////
//////////////////获取商品属性//////////////////
JSONArray attrSaleList = propObj.getJSONArray("attrSaleList");
for (int i = 0; i < attrSaleList.size(); i++) {
JSONArray attributeValueList = attrSaleList.getJSONObject(i).getJSONArray("attributeValueList");
//商品属性
Set<ProductProp> propSet = new HashSet<>();
for (int j = 0; j < attributeValueList.size(); j++) {
ProductProp productProp = new ProductProp();
//获取图片,拿第一张
if (attributeValueList.getJSONObject(j).get("itemAttributeValueImageList") != null && !"null".equalsIgnoreCase(attributeValueList.getJSONObject(j).getString("itemAttributeValueImageList"))) {
JSONArray itemAttributeValueImageList = attributeValueList.getJSONObject(j).getJSONArray("itemAttributeValueImageList");
productProp.setImage(itemAttributeValueImageList.getJSONObject(0).getString("picUrl"));
}
productProp.setPropName(attributeValueList.getJSONObject(j).getString("attributeValueName"));
productProp.setPropId(attributeValueList.getJSONObject(j).getString("code"));
propSet.add(productProp);
}
String attributeFrontName = attrSaleList.getJSONObject(i).getString("attributeFrontName");
if (productPropSet.get(attributeFrontName) == null) {
productPropSet.put(attributeFrontName, propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get(attributeFrontName);
propSet.addAll(oldPropSet);
productPropSet.put(attributeFrontName, propSet);
}
}
//////////////////获取商品属性 END//////////////////
itemInfo.setItemId(propObj.getString("style"));
if (propObj.get("itemImageList") != null && !"null".equalsIgnoreCase(propObj.getString("itemImageList"))) {
JSONArray itemImageList = propObj.getJSONArray("itemImageList");
if (!itemImageList.isEmpty()) {
String pic = itemImageList.getJSONObject(0).getString("picUrl");
//取第一张当作主图
itemInfo.setPic(pic);
}
}
itemInfo.setShopName(PlatformEnum.GAP.getLabel());
itemInfo.setShopUrl("https://www.gap.cn/");
itemInfo.setTitle(propObj.getString("title"));
}
String minPrice = dataMap.getString("minPrice");
String maxPrice = dataMap.getString("maxPrice");
//转换汇率
minPrice = exchangeRate(minPrice);
maxPrice = exchangeRate(maxPrice);
//一口价
productResponse.setPrice(minPrice + "-" + maxPrice);
//一口价
productResponse.setSalePrice(minPrice + "-" + maxPrice);
//没有库存信息 需要另外获取
productResponse.setStockFlag(false);
//有商品属性
productResponse.setPropFlag(true);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setPromotionList(promotionList);
productResponse.setItemInfo(itemInfo);
productResponse.setPlatform(PlatformEnum.GAP.getValue());
productResponse.setProductPropSet(productPropSet);
return productResponse;
}
/**
* 格式化 nike 返回数据
*
* @param dataMap
* @return
*/
public static ProductResponse formatNikeProductResponse(JSONObject dataMap) {
ProductResponse productResponse = new ProductResponse();
//nike 基本是 颜色、尺码属性
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
//原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
//促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
//库存
DynStock dynStock = new DynStock();
//其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
//商品基本信息
ItemInfo itemInfo = new ItemInfo();
JSONObject threadObj = dataMap.getJSONObject("Threads");
JSONObject productsObj = threadObj.getJSONObject("products");
Set es = productsObj.entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry<String, JSONObject> entry = (Map.Entry) it.next();
String skuStr = ";";
String modelCode = entry.getKey();
skuStr = skuStr + modelCode + ";";
JSONObject itemDetail = entry.getValue();
////////////////////////////////////获取价格和商品属性////////////////////////////////////////////
String fullPrice = itemDetail.getString("fullPrice");
//转换汇率
fullPrice = exchangeRate(fullPrice);
String currentPrice = itemDetail.getString("currentPrice");
//转换汇率
currentPrice = exchangeRate(currentPrice);
productResponse.setPrice(fullPrice);
JSONArray skusArr = itemDetail.getJSONArray("skus");
//获取商品尺码属性,同时记录下skuid和尺码关系
Map<String, String> sizeSkuIdMapping = new HashMap<>(16);
for (int i = 0; i < skusArr.size(); i++) {
String skuId = skusArr.getJSONObject(i).getString("skuId");
/////////////////////////获取商品尺码属性////////////////////
//商品属性
Set<ProductProp> sizePropSet = new HashSet<>();
ProductProp productProp = new ProductProp();
String localizedSize = skusArr.getJSONObject(i).getString("localizedSize");
String localizedSizePrefix = skusArr.getJSONObject(i).getString("localizedSizePrefix");
//因为尺码一样的时候skuid却不一样,这里只能赋予一个propid,否则后面去重不了
String customizeId = KeyConstant.CUSTOMIZE_ID + localizedSize + localizedSizePrefix;
sizeSkuIdMapping.put(skuId, customizeId);
productProp.setPropId(customizeId);
productProp.setPropName(localizedSizePrefix + " " + localizedSize);
sizePropSet.add(productProp);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSet.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSet);
}
/////////////////////////获取商品尺码属性 END////////////////////
}
////////////////////////////////////获取价格//////////////////////////////////
for (int i = 0; i < skusArr.size(); i++) {
String skuId = skusArr.getJSONObject(i).getString("skuId");
String customizeId = sizeSkuIdMapping.get(skuId);
OriginalPrice originalPrice = new OriginalPrice();
originalPrice.setSkuStr(skuStr + customizeId + ";");
originalPrice.setPrice(fullPrice);
originalPriceList.add(originalPrice);
if (itemDetail.getBoolean("discounted")) {
productResponse.setPromotionFlag(true);
productResponse.setSalePrice(currentPrice);
ProductPromotion productPromotion = new ProductPromotion();
productPromotion.setSkuStr(skuStr + customizeId + ";");
productPromotion.setPrice(fullPrice);
promotionList.add(productPromotion);
}
}
////////////////////////////////////获取价格 END//////////////////////////////////
/////////////////////////////////////获取价格和商品属性 END////////////////////////////////////////////
////////////////////////////////////获取库存 ////////////////////////////////////////////
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
JSONArray availableSkusArr = itemDetail.getJSONArray("availableSkus");
for (int i = 0; i < availableSkusArr.size(); i++) {
String skuId = availableSkusArr.getJSONObject(i).getString("skuId");
String customizeId = sizeSkuIdMapping.get(skuId);
ProductSkuStock productSkuStock = new ProductSkuStock();
productSkuStock.setSellableQuantity(999);
productSkuStock.setSkuStr(skuStr + customizeId + ";");
productSkuStockList.add(productSkuStock);
}
dynStock.setProductSkuStockList(productSkuStockList);
////////////////////////////////////获取库存 END////////////////////////////////////////////
////////////////////////////////////获取商品颜色属性////////////////////////////////////////////
//商品属性
Set<ProductProp> propSet = new HashSet<>();
ProductProp productProp = new ProductProp();
String colorDescription = itemDetail.getString("colorDescription");
String firstImageUrl = itemDetail.getString("firstImageUrl");
productProp.setPropId(modelCode);
productProp.setPropName(colorDescription);
productProp.setImage(firstImageUrl);
propSet.add(productProp);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSet.addAll(oldPropSet);
productPropSet.put("颜色", propSet);
}
////////////////////////////////////获取商品属性 END////////////////////////////////////////////
}
JSONObject globalObj = dataMap.getJSONObject("global");
JSONObject metaTagsObj = globalObj.getJSONObject("metaTags");
JSONArray metaArr = metaTagsObj.getJSONArray("meta");
for (int i = 0; i < metaArr.size(); i++) {
if (metaArr.getJSONObject(i).get("property") != null) {
String propertyValue = metaArr.getJSONObject(i).getString("property");
if ("og:title".equalsIgnoreCase(propertyValue)) {
itemInfo.setTitle(metaArr.getJSONObject(i).getString("content"));
}
if ("og:image".equalsIgnoreCase(propertyValue)) {
itemInfo.setPic(metaArr.getJSONObject(i).getString("content"));
}
}
}
itemInfo.setShopUrl("https://www.nike.com/cn/");
itemInfo.setShopName(PlatformEnum.NIKE.getLabel());
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.NIKE.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
/**
* 格式化 afric-eshop 返回数据
*
* @param resultObj
* @return
*/
public static ProductResponse formatAfricaShopProductResponse(JSONObject resultObj) {
ProductResponse productResponse = new ProductResponse();
//原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
//促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
//库存
DynStock dynStock = new DynStock();
//其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
//nike 基本是 颜色、尺码属性
Map<String, Set<ProductProp>> productPropSet = new HashMap<>();
//商品基本信息
ItemInfo itemInfo = new ItemInfo();
JSONArray variantsArray = resultObj.getJSONArray("variants");
//属性
JSONArray optionsArray = resultObj.getJSONArray("options");
for (int i = 0; i < variantsArray.size(); i++) {
//属性
JSONArray itemOptionsArray = variantsArray.getJSONObject(i).getJSONArray("options");
//没有属性的时候,会返回 Default Title
if ("Default Title".equalsIgnoreCase(itemOptionsArray.getString(0))) {
break;
}
String skuStr = ";";
for (int m = 0; m < itemOptionsArray.size(); m++) {
skuStr = skuStr + KeyConstant.CUSTOMIZE_ID + itemOptionsArray.getString(m) + ";";
}
///////////////////原始价////////////////////////////////////
OriginalPrice originalPrice = new OriginalPrice();
String price = variantsArray.getJSONObject(i).getString("price");
BigDecimal priceOld = new BigDecimal(price);
BigDecimal div = new BigDecimal("100");
BigDecimal priceNew = priceOld.divide(div, 2, BigDecimal.ROUND_DOWN);
originalPrice.setPrice(priceNew.toString());
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
///////////////////原始价 END////////////////////////////////
////////////////////////////////////获取库存 ////////////////////////////////////////////
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
ProductSkuStock productSkuStock = new ProductSkuStock();
productSkuStock.setSellableQuantity(999);
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
////////////////////////////////////获取库存 END////////////////////////////////////////////
//获取所有的属性
for (int j = 0; j < optionsArray.size(); j++) {
////////////////////////////////////获取商品属性////////////////////////////////////////////
//商品属性
Set<ProductProp> propSet = new HashSet<>();
ProductProp productProp = new ProductProp();
productProp.setPropId(KeyConstant.CUSTOMIZE_ID + itemOptionsArray.getString(j));
productProp.setPropName(itemOptionsArray.getString(j));
propSet.add(productProp);
if (productPropSet.get(optionsArray.getString(j)) == null) {
productPropSet.put(optionsArray.getString(j), propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get(optionsArray.getString(j));
propSet.addAll(oldPropSet);
productPropSet.put(optionsArray.getString(j), propSet);
}
////////////////////////////////////获取属性 END////////////////////////////////////////////
}
}
itemInfo.setItemId(resultObj.getString("id"));
//取第一张
itemInfo.setPic(resultObj.getString("featured_image"));
itemInfo.setShopName(PlatformEnum.AfriEshop.getValue());
itemInfo.setShopUrl("https://www.afri-eshop.com/");
itemInfo.setTitle(resultObj.getString("title"));
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.AfriEshop.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
String price = resultObj.getString("price");
BigDecimal priceOld = new BigDecimal(price);
BigDecimal div = new BigDecimal("100");
BigDecimal priceNew = priceOld.divide(div, 2, BigDecimal.ROUND_DOWN);
productResponse.setPrice(priceNew.toString());
return productResponse;
}
/**
* 格式化 PullAndBear 返回数据
*
* @param dataMap 主要的 json 数据
* @param pId 商品链接的 id
* @return 格式化后的数据
* @see com.diaoyun.zion.chinafrica.bis.impl.PullandbearSpider
*/
public static ProductResponse formatPullAndBearProductResponse(JSONObject dataMap, String pId) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 商品基本信息
ItemInfo itemInfo = new ItemInfo();
// 取 bundleProductSummaries 的节点对象
JSONObject bundleProductSummariesObj = dataMap.getJSONArray("bundleProductSummaries").getJSONObject(0);
//////////////////////////////////// 获取商品基本信息 ////////////////////////////////////////////
itemInfo.setShopName("PullAndBear");
itemInfo.setShopUrl("https://www.pullandbear.cn/");
itemInfo.setItemId(pId);
itemInfo.setTitle(bundleProductSummariesObj.getString("name"));
//////////////////////////////////// 获取商品基本信息End(图片下取) ////////////////////////////////////////////
// 取 colors 数组节点
JSONArray colorsArr = bundleProductSummariesObj.getJSONObject("detail").getJSONArray("colors");
Set<ProductProp> propSetColor = new HashSet<>(16);
Set<ProductProp> sizePropSetSize = new HashSet<>(16);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
productResponse.setStockFlag(true);
for (int i = 0; i < colorsArr.size(); i++) {
JSONObject colorsObj = colorsArr.getJSONObject(i);
//////////////////////////////////// 获取商品颜色与图片属性 ////////////////////////////////////////////
ProductProp productPropColor = new ProductProp();
JSONObject imageObj = colorsObj.getJSONObject("image");
String imageUrl = "https://static.pullandbear.cn/2/photos/"
+ imageObj.getString("url")
+ "_2_1_8.jpg?t="
+ imageObj.getString("timestamp");
String colorNo = colorsObj.getString("id");
String color = colorsObj.getString("name");
productPropColor.setPropId(colorNo);
productPropColor.setPropName(color);
productPropColor.setImage(imageUrl);
if (i == 0) {
itemInfo.setPic(imageUrl);
}
propSetColor.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSetColor);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSetColor.addAll(oldPropSet);
productPropSet.put("颜色", propSetColor);
}
//////////////////////////////////// 获取商品颜色与图片属性 END ////////////////////////////////////////////
// 取 siezes 对象数组
JSONArray sizesArr = colorsObj.getJSONArray("sizes");
for (int j = 0; j < sizesArr.size(); j++) {
JSONObject sizesObj = sizesArr.getJSONObject(j);
///////////////////////// 获取商品尺码属性 ////////////////////
ProductProp productPropSize = new ProductProp();
String sizeNo = sizesObj.getString("sku");
String size = sizesObj.getString("name");
productPropSize.setPropName(size);
productPropSize.setPropId(sizeNo);
sizePropSetSize.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSetSize);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSetSize.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSetSize);
}
///////////////////////// 获取商品尺码属性 END////////////////////
// 商品的库存id
String skuStr = ";" + colorNo + ";" + sizeNo + ";";
//////////////////////////////////// 获取库存 ////////////////////////////////////////////
// 设置:商品包含库存信息
ProductSkuStock productSkuStock = new ProductSkuStock();
productSkuStock.setSkuStr(skuStr);
productSkuStock.setSellableQuantity(999);
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
productSkuStockList.add(productSkuStock);
//////////////////////////////////// 获取库存 END/////////////////////////////////////////
//////////////////////////////////// 获取原始价 //////////////////////////////////
OriginalPrice originalPrice = new OriginalPrice();
// 获取商品的原始价
String fullPrice = sizesObj.getString("price");
BigDecimal priceOld = new BigDecimal(fullPrice);
BigDecimal div = new BigDecimal("100");
BigDecimal priceNew = priceOld.divide(div, 2, BigDecimal.ROUND_DOWN);
// TODO 转换汇率,目前商品单位是人民币
fullPrice = SpiderUtil.exchangeRate(priceNew.toString());
originalPrice.setSkuStr(skuStr);
originalPrice.setPrice(fullPrice);
originalPriceList.add(originalPrice);
productResponse.setPrice(fullPrice);
productResponse.setSalePrice(fullPrice + "-" + fullPrice);
//////////////////////////////////// 获取原始价 END//////////////////////////////////
}
dynStock.setProductSkuStockList(productSkuStockList);
}
// 按照一下顺序进行 json 数据的填充
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.PULLANDBEAR.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.util.*;
/**
* UnderArmour 爬虫数据解析
* @see com.diaoyun.zion.chinafrica.bis.impl.UnderArmourSpider
* @author 爱酱油不爱醋
*/
public class UnderArmourSpiderParse {
/**
* 格式化返回数据
* @param content 主要的页面数据
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(String content) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 解析成 Document 对象
Document document = Jsoup.parse(content);
// 获取商品的标题、价格、颜色名称及id、尺码名称及id
String pId = document.select("span[class=e-color-show]").text();
String pTitle = document.select("h3[class=commo-name]").text();
String fullPrice = document.select("p[class=commo-price]").text().replaceAll("¥", "");
Elements colorEle = document.select("ul[class=color-choice float-clearfix e-color-choice]").select("li");
List<String> pColorList = colorEle.eachText();
List<String> pColorNoList = colorEle.eachAttr("itemcode");
Elements sizeEle = document.select("ul[class=size-choice float-clearfix e-size-choice]").select("li");
List<String> pSizeList = sizeEle.eachText();
List<String> pSizeNoList = sizeEle.eachAttr("skuid");
//////////////////////////////////// 获取商品基本信息 ////////////////////////////
ItemInfo itemInfo = new ItemInfo();
itemInfo.setShopName(PlatformEnum.UNDERARMOUR.getLabel());
itemInfo.setShopUrl("https://www.adidas.com");
itemInfo.setItemId(pId);
itemInfo.setTitle(pTitle);
itemInfo.setPic("https://underarmour.scene7.com/is/image/Underarmour/V5-" + pId + "_FC_Main");
//////////////////////////////////// 获取商品基本信息End /////////////////////////
//////////////////////////////////// 获取商品颜色属性 ////////////////////////////
for (int i = 0; i < pColorList.size(); i++) {
Set<ProductProp> propSet = new HashSet<>(16);
ProductProp productPropColor = new ProductProp();
productPropColor.setPropName(pColorList.get(i));
productPropColor.setPropId(pColorNoList.get(i));
productPropColor.setImage("https://underarmour.scene7.com/is/image/Underarmour/V5-" + pColorNoList.get(i) + "_FC_Main");
propSet.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSet.addAll(oldPropSet);
productPropSet.put("颜色", propSet);
}
}
//////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
///////////////////////// 获取商品尺码属性 ////////////////////
for (int i = 0; i < pSizeList.size(); i++) {
Set<ProductProp> sizePropSet = new HashSet<>();
ProductProp productPropSize = new ProductProp();
productPropSize.setPropId(pSizeNoList.get(i));
productPropSize.setPropName(pSizeList.get(i));
sizePropSet.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSet.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSet);
}
}
///////////////////////// 获取商品尺码属性 END////////////////////
//////////////////////////////////// 获取库存与原始价 ////////////////////////////////////////////
for (String pColorNo : pColorNoList) {
for (String pSizeNo : pSizeNoList) {
// 设置库存id
String skuStr = ";" + pColorNo + ";" + pSizeNo + ";";
// 设置:商品包含库存信息
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
ProductSkuStock productSkuStock = new ProductSkuStock();
OriginalPrice originalPrice = new OriginalPrice();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
// 设置:可用库存值,未有可用的库存数据
productSkuStock.setSellableQuantity(999);
// 设置:库存对应的id
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
// TODO 转换汇率,目前商品单位是人民币
String originalFullPrice = SpiderUtil.exchangeRate(fullPrice);
originalPrice.setPrice(originalFullPrice);
productResponse.setPrice(originalFullPrice);
productResponse.setSalePrice(originalFullPrice + "-" + originalFullPrice);
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
}
}
//////////////////////////////////// 获取库存与原始价 END///////////////////////////////
// 按照一下顺序进行 json 数据的填充
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.UNDERARMOUR.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.*;
/**
* Uniqlo 数据爬虫
* @see com.diaoyun.zion.chinafrica.bis.impl.UniqloSpider
* @author 爱酱油不爱醋
*/
public class UniqloSpiderParse {
/**
* 返回格式化数据
* @param dataMap 调用优衣库网页接口接收的主要商品数据
* @param pId 商品链接截取的商品id
* @return
*/
public static ProductResponse formatProductResponse(JSONObject dataMap, JSONObject priceJson, String pId) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 商品基本信息
ItemInfo itemInfo = new ItemInfo();
// 取 summary 节点对象
JSONObject summaryObj = dataMap.getJSONObject("summary");
//////////////////////////////////// 获取商品基本信息 ////////////////////////////////////////////
itemInfo.setShopName(PlatformEnum.UNIQLO.getLabel());
itemInfo.setShopUrl("https://www.uniqlo.cn/product-detail.html");
itemInfo.setItemId(pId);
itemInfo.setTitle(summaryObj.getString("fullName"));
//////////////////////////////////// 获取商品基本信息End(图片下取) ////////////////////////////////////////////
// 取 rows 节点数组
JSONArray rowsArr = dataMap.getJSONArray("rows");
for (int i = 0; i < rowsArr.size(); i++) {
JSONObject rowsObj = rowsArr.getJSONObject(i);
//////////////////////////////////// 获取商品颜色属性 ////////////////////////////////////////////
Set<ProductProp> propSetColor = new HashSet<>(16);
ProductProp productPropColor = new ProductProp();
// 颜色id
String colorNo = rowsObj.getString("colorNo");
productPropColor.setPropId(colorNo);
// 颜色名
String colorName = rowsObj.getString("style");
productPropColor.setPropName(colorName);
// 颜色图片
String imageUrl = "https://www.uniqlo.cn/hmall/test/" + pId + "/sku/561/" + colorNo + ".jpg";
productPropColor.setImage(imageUrl);
if (i == 0) {
itemInfo.setPic(imageUrl);
}
propSetColor.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSetColor);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSetColor.addAll(oldPropSet);
productPropSet.put("颜色", propSetColor);
}
//////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
///////////////////////// 获取商品尺码属性 ////////////////////
Set<ProductProp> sizePropSetSize = new HashSet<>();
ProductProp productPropSize = new ProductProp();
String size = rowsObj.getString("sizeText");
productPropSize.setPropName(size);
String sizeNo = rowsObj.getString("skuId");
productPropSize.setPropId(sizeNo);
sizePropSetSize.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSetSize);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSetSize.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSetSize);
}
///////////////////////// 获取商品尺码属性 END////////////////////
String skuStr = ";" + colorNo + ";" + sizeNo + ";";
//////////////////////////////////// 获取库存 ////////////////////////////////////////////
// 设置:商品包含库存信息
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
ProductSkuStock productSkuStock = new ProductSkuStock();
// 设置:可用库存值,Uniqlo 未有可用的库存数据
productSkuStock.setSellableQuantity(999);
// 设置:库存对应的id
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
//////////////////////////////////// 获取库存 END/////////////////////////////////////////
//////////////////////////////////// 获取原始价 //////////////////////////////////
OriginalPrice originalPrice = new OriginalPrice();
// 获取商品的原始价
String fullPrice = summaryObj.getString("originPrice");
// TODO 转换汇率,目前商品单位是人民币
fullPrice = SpiderUtil.exchangeRate(fullPrice);
originalPrice.setPrice(fullPrice);
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
//////////////////////////////////// 获取原始价 END//////////////////////////////////
//////////////////////////////////// 获取促销价 //////////////////////////////////
// 取 summary 节点对象
JSONObject summaryPriceObj = priceJson.getJSONArray("resp").getJSONObject(0).getJSONObject("summary");
ProductPromotion productPromotion = new ProductPromotion();
// 获取商品的促销价
String promotionPrice = summaryPriceObj.getString("maxPrice");
// TODO 转换汇率,目前商品单位是人民币
promotionPrice = SpiderUtil.exchangeRate(promotionPrice);
productPromotion.setSkuStr(skuStr);
productPromotion.setPrice(promotionPrice);
productResponse.setPrice(promotionPrice);
productResponse.setSalePrice(promotionPrice + "-" + fullPrice);
promotionList.add(productPromotion);
//////////////////////////////////// 获取促销价 END//////////////////////////////////
}
// 按照一下顺序进行 json 数据的填充
productResponse.setPropFlag(true);
productResponse.setPromotionFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.UNIQLO.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.*;
import static com.diaoyun.zion.master.util.spider.SpiderUtil.exchangeRate;
/**
* UrbanRevivo 爬虫数据解析
* @see com.diaoyun.zion.chinafrica.bis.impl.UrbanRevivoSpider
* @author 爱酱油不爱醋
*/
public class UrbanRevivoSpiderParse {
/**
* 格式化返回数据
* @param dataMap 主要的 json 数据
* @param pId 截取的商品 id
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(JSONObject dataMap, String pId) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 取 data 数据节点
JSONObject dataObj = dataMap.getJSONObject("data");
//////////////////////////////////// 获取商品基本信息 ////////////////////////////////////////////
ItemInfo itemInfo = new ItemInfo();
itemInfo.setShopName(PlatformEnum.URBANREVIVO.getLabel());
itemInfo.setShopUrl("http://www.ur.cn/index.html");
itemInfo.setItemId(pId);
itemInfo.setTitle(dataObj.getString("name"));
itemInfo.setPic("https://gw-img.ur.com.cn//" + dataObj.getString("image"));
//////////////////////////////////// 获取商品基本信息End////////////////////////////////////////////
// 取 colors 节点数组
JSONArray colorsArr = dataObj.getJSONArray("colors");
for (int i = 0; i < colorsArr.size(); i++) {
JSONObject colorsObj = colorsArr.getJSONObject(i);
// 获取图片路径
String imgUrl = "https://gw-img.ur.com.cn//" + colorsObj.getString("image");
//////////////////////////////////// 获取商品颜色属性 ////////////////////////////////////////////
Set<ProductProp> propSet = new HashSet<>(16);
ProductProp productPropColor = new ProductProp();
String colorNo = colorsObj.getString("productColorId");
productPropColor.setPropId(colorNo);
productPropColor.setPropName(colorsObj.getString("aliasName"));
productPropColor.setImage(imgUrl);
propSet.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSet.addAll(oldPropSet);
productPropSet.put("颜色", propSet);
}
//////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
// 取 skus 节点数组
JSONArray skusArr = colorsObj.getJSONArray("skus");
for (int j = 0; j < skusArr.size(); j++) {
JSONObject skusObj = skusArr.getJSONObject(j);
///////////////////////// 获取商品尺码属性 ////////////////////
Set<ProductProp> sizePropSet = new HashSet<>();
ProductProp productPropSize = new ProductProp();
String sizeNo = skusObj.getString("barCode");
productPropSize.setPropId(sizeNo);
productPropSize.setPropName(skusObj.getString("sizeAlias"));
sizePropSet.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSet.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSet);
}
///////////////////////// 获取商品尺码属性 END////////////////////
// 设置库存id
String skuStr = ";" + colorNo + ";" + sizeNo + ";";
//////////////////////////////////// 获取库存 ////////////////////////////////////////////
// 设置:商品包含库存信息
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
ProductSkuStock productSkuStock = new ProductSkuStock();
// 设置:可用库存值,未有可用的库存数据
productSkuStock.setSellableQuantity(999);
// 设置:库存对应的id
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
//////////////////////////////////// 获取库存 END/////////////////////////////////////////
//////////////////////////////////// 获取原始价 //////////////////////////////////
OriginalPrice originalPrice = new OriginalPrice();
// 获取商品的原始价
String fullPrice = dataObj.getString("tagPrice");
// TODO 转换汇率,目前商品单位是人民币
fullPrice = exchangeRate(fullPrice);
originalPrice.setPrice(fullPrice);
productResponse.setPrice(fullPrice);
productResponse.setSalePrice(fullPrice + "-" + fullPrice);
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
//////////////////////////////////// 获取原始价 END//////////////////////////////////
}
}
// 按照一下顺序进行 json 数据的填充
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.URBANREVIVO.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.util.*;
import java.util.regex.Pattern;
/**
* Vans(范斯) 爬虫数据解析
* @see com.diaoyun.zion.chinafrica.bis.impl.VansSpider
* @author 爱酱油不爱醋
*/
public class VansSpiderParse {
/**
* 格式化返回数据
* @param content 主要的页面数据
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(String content, String pId, String pTitle) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
// 解析成 Document 对象
Document document = Jsoup.parse(content);
Elements colorUrlEle = document.select("div[class=pro-color]").select("a");
// 价格
String fullPrice = Pattern.compile("[^0-9]").matcher(document.select("span[id=spec_price]").text()).replaceAll("").trim();
// 颜色Id
List<String> colorNoList = colorUrlEle.eachAttr("data-product-id");
// 颜色名称
List<String> colorList = colorUrlEle.eachText();
// 颜色图片
List<String> imageList = colorUrlEle.select("img").eachAttr("src");
//////////////////////////////////// 获取商品基本信息 ////////////////////////////
ItemInfo itemInfo = new ItemInfo();
itemInfo.setShopName(PlatformEnum.VANS.getLabel());
itemInfo.setShopUrl("https://www.vans.com");
itemInfo.setItemId(pId);
itemInfo.setTitle(pTitle);
itemInfo.setPic(imageList.get(0));
//////////////////////////////////// 获取商品基本信息End /////////////////////////
// //////////////////////////////////// 获取商品颜色属性 ////////////////////////////
// for (int i = 0; i < pColorList.size(); i++) {
// Set<ProductProp> propSet = new HashSet<>(16);
// ProductProp productPropColor = new ProductProp();
// productPropColor.setPropName(pColorList.get(i));
// productPropColor.setPropId(pColorNoList.get(i));
// productPropColor.setImage("https://underarmour.scene7.com/is/image/Underarmour/V5-" + pColorNoList.get(i) + "_FC_Main");
// propSet.add(productPropColor);
// if (productPropSet.get("颜色") == null) {
// productPropSet.put("颜色", propSet);
// } else {
// Set<ProductProp> oldPropSet = productPropSet.get("颜色");
// propSet.addAll(oldPropSet);
// productPropSet.put("颜色", propSet);
// }
// }
// //////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
//
// ///////////////////////// 获取商品尺码属性 ////////////////////
// for (int i = 0; i < pSizeList.size(); i++) {
// Set<ProductProp> sizePropSet = new HashSet<>();
// ProductProp productPropSize = new ProductProp();
// productPropSize.setPropId(pSizeNoList.get(i));
// productPropSize.setPropName(pSizeList.get(i));
// sizePropSet.add(productPropSize);
// if (productPropSet.get("尺码") == null) {
// productPropSet.put("尺码", sizePropSet);
// } else {
// Set<ProductProp> oldPropSet = productPropSet.get("尺码");
// sizePropSet.addAll(oldPropSet);
// productPropSet.put("尺码", sizePropSet);
// }
//
// }
// ///////////////////////// 获取商品尺码属性 END////////////////////
//
// //////////////////////////////////// 获取库存与原始价 ////////////////////////////////////////////
// for (String pColorNo : pColorNoList) {
// for (String pSizeNo : pSizeNoList) {
// // 设置库存id
// String skuStr = ";" + pColorNo + ";" + pSizeNo + ";";
// // 设置:商品包含库存信息
// productResponse.setStockFlag(true);
// List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
// ProductSkuStock productSkuStock = new ProductSkuStock();
// OriginalPrice originalPrice = new OriginalPrice();
// if (productSkuStockList == null) {
// productSkuStockList = new ArrayList<>();
// }
//
// // 设置:可用库存值,未有可用的库存数据
// productSkuStock.setSellableQuantity(999);
// // 设置:库存对应的id
// productSkuStock.setSkuStr(skuStr);
// productSkuStockList.add(productSkuStock);
// dynStock.setProductSkuStockList(productSkuStockList);
//
// // TODO 转换汇率,目前商品单位是人民币
// String originalFullPrice = SpiderUtil.exchangeRate(fullPrice);
// originalPrice.setPrice(originalFullPrice);
// productResponse.setPrice(originalFullPrice);
// productResponse.setSalePrice(originalFullPrice + "-" + originalFullPrice);
// originalPrice.setSkuStr(skuStr);
// originalPriceList.add(originalPrice);
// }
// }
// //////////////////////////////////// 获取库存与原始价 END///////////////////////////////
// 按照一下顺序进行 json 数据的填充
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.UNDERARMOUR.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
package com.diaoyun.zion.master.util.spider;
import com.diaoyun.zion.chinafrica.enums.PlatformEnum;
import com.diaoyun.zion.chinafrica.vo.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.math.BigDecimal;
import java.util.*;
import static com.diaoyun.zion.master.util.spider.SpiderUtil.exchangeRate;
/**
* Zara 爬虫数据解析
* @see com.diaoyun.zion.chinafrica.bis.impl.ZaraSpider
* @author 爱酱油不爱醋
*/
public class ZaraSpiderParse {
/**
* 获取主要数据
* @param content 商品的 HTML 页面
* @return 截取后的主要数据
*/
public static JSONObject getJsonData(String content) {
int labelHeadIndex = content.indexOf("dataLayer");
int labelTailIndex = content.lastIndexOf(";window.zara.viewPayload");
String abv = content.substring(labelHeadIndex, labelTailIndex).replace("dataLayer = ", "");
JSONObject dataMap= JSONObject.fromObject(abv);
return dataMap;
}
/**
* 格式化返回数据
* @param dataMap 主要的 json 数据
* @return 格式化后的数据
*/
public static ProductResponse formatProductResponse(JSONObject dataMap) {
// 声明封装类
ProductResponse productResponse = new ProductResponse();
// 属性:Zara 的商品属性有颜色、尺码
Map<String, Set<ProductProp>> productPropSet = new HashMap<>(16);
// 原始价
List<OriginalPrice> originalPriceList = new ArrayList<>();
// 促销价格
List<ProductPromotion> promotionList = new ArrayList<>();
// 库存
DynStock dynStock = new DynStock();
// 其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock.setSellableQuantity(9999);
//////////////////////////////////// 获取商品基本信息 ////////////////////////////////////////////
ItemInfo itemInfo = new ItemInfo();
itemInfo.setShopName("Zara");
itemInfo.setShopUrl(dataMap.getString("backUrl"));
JSONObject productObj = dataMap.getJSONObject("product");
itemInfo.setItemId(productObj.getString("id"));
itemInfo.setTitle(productObj.getString("name"));
//////////////////////////////////// 获取商品基本信息End(图片下取) ////////////////////////////////////////////
// 取 colors 节点数组
JSONArray colorsArr = productObj.getJSONObject("detail").getJSONArray("colors");
for (int i = 0; i < colorsArr.size(); i++) {
JSONObject colorsObj = colorsArr.getJSONObject(i);
// 取 detailImagesArr 节点数组第一个对象
JSONObject detailImagesObj_0 = colorsObj.getJSONArray("detailImages").getJSONObject(0);
// 处理图片 参考路径:http://static.zara.cn/photos///2019/I/0/1/p/0858/457/800/17/w/1920/0858457800_1_1_1.jpg?ts=1570720340221
String imageUrl = "http://static.zara.cn/photos//"
+ detailImagesObj_0.getString("path")
+ "w/1920/"
+ detailImagesObj_0.getString("name")
+ "_1.jpg?ts="
+ detailImagesObj_0.getString("timestamp");
if (i == 0) {
// 商品基本信息--设置:图片
itemInfo.setPic(imageUrl);
}
//////////////////////////////////// 获取商品颜色属性 ////////////////////////////////////////////
Set<ProductProp> propSet = new HashSet<>(16);
ProductProp productPropColor = new ProductProp();
// 颜色描述
productPropColor.setPropId(colorsObj.getString("productId"));
productPropColor.setPropName(colorsObj.getString("name"));
productPropColor.setImage(imageUrl);
propSet.add(productPropColor);
if (productPropSet.get("颜色") == null) {
productPropSet.put("颜色", propSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("颜色");
propSet.addAll(oldPropSet);
productPropSet.put("颜色", propSet);
}
//////////////////////////////////// 获取商品颜色属性 END ////////////////////////////////////////////
// 取 sizes 节点数组
JSONArray sizesArr = colorsArr.getJSONObject(i).getJSONArray("sizes");
for (int j = 0; j < sizesArr.size(); j++) {
JSONObject sizesObj = sizesArr.getJSONObject(j);
// 库存对应的id(Zara 中以颜色id + 尺码id)
String skuStr = ";" + colorsObj.getString("productId") + ";" + sizesObj.getString("sku") + ";";
//////////////////////////////////// 获取库存 ////////////////////////////////////////////
// 设置:商品包含库存信息
productResponse.setStockFlag(true);
List<ProductSkuStock> productSkuStockList = dynStock.getProductSkuStockList();
if (productSkuStockList == null) {
productSkuStockList = new ArrayList<>();
}
ProductSkuStock productSkuStock = new ProductSkuStock();
// 设置:可用库存值,Zara 未有可用的库存数据
productSkuStock.setSellableQuantity(999);
// 设置:库存对应的id
productSkuStock.setSkuStr(skuStr);
productSkuStockList.add(productSkuStock);
dynStock.setProductSkuStockList(productSkuStockList);
//////////////////////////////////// 获取库存 END/////////////////////////////////////////
//////////////////////////////////// 获取原始价 //////////////////////////////////
OriginalPrice originalPrice = new OriginalPrice();
// 获取商品的原始价
String fullPrice = sizesObj.getString("price");
BigDecimal priceOld=new BigDecimal(fullPrice);
BigDecimal div = new BigDecimal("100");
BigDecimal priceNew = priceOld.divide(div, 2, BigDecimal.ROUND_DOWN);
// TODO 转换汇率,目前商品单位是人民币
fullPrice = exchangeRate(priceNew.toString());
originalPrice.setPrice(fullPrice);
productResponse.setPrice(fullPrice);
productResponse.setSalePrice(fullPrice + "-" + fullPrice);
originalPrice.setSkuStr(skuStr);
originalPriceList.add(originalPrice);
//////////////////////////////////// 获取原始价 END//////////////////////////////////
///////////////////////// 获取商品尺码属性 ////////////////////
Set<ProductProp> sizePropSet = new HashSet<>();
ProductProp productPropSize = new ProductProp();
String size = sizesObj.getString("name");
productPropSize.setPropId(sizesObj.getString("sku"));
productPropSize.setPropName(size);
sizePropSet.add(productPropSize);
if (productPropSet.get("尺码") == null) {
productPropSet.put("尺码", sizePropSet);
} else {
Set<ProductProp> oldPropSet = productPropSet.get("尺码");
sizePropSet.addAll(oldPropSet);
productPropSet.put("尺码", sizePropSet);
}
///////////////////////// 获取商品尺码属性 END////////////////////
}
}
// 按照一下顺序进行 json 数据的填充
productResponse.setPropFlag(true);
productResponse.setProductPropSet(productPropSet);
productResponse.setPlatform(PlatformEnum.ZARA.getValue());
productResponse.setPromotionList(promotionList);
productResponse.setOriginalPriceList(originalPriceList);
productResponse.setItemInfo(itemInfo);
productResponse.setDynStock(dynStock);
return productResponse;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论