Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
Z
zion
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
1
合并请求
1
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
zhengfg
zion
Commits
e027f6dc
提交
e027f6dc
authored
12月 07, 2019
作者:
梁业锦
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
重写 AfriesHop 的爬虫逻辑
上级
7fa35391
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
139 行增加
和
18 行删除
+139
-18
AfricaShopItemSpider.java
...iaoyun/zion/chinafrica/bis/impl/AfricaShopItemSpider.java
+139
-18
没有找到文件。
src/main/java/com/diaoyun/zion/chinafrica/bis/impl/AfricaShopItemSpider.java
浏览文件 @
e027f6dc
...
@@ -21,9 +21,11 @@ import java.util.concurrent.ExecutionException;
...
@@ -21,9 +21,11 @@ import java.util.concurrent.ExecutionException;
import
java.util.concurrent.TimeoutException
;
import
java.util.concurrent.TimeoutException
;
/**
/**
* afri
-
eshop 数据爬虫
* afrieshop 数据爬虫
*
*
* @author G
* @author G
* @version 1.1
* @author 爱酱油不爱醋
*/
*/
@Component
(
"africaShopItemSpider"
)
@Component
(
"africaShopItemSpider"
)
public
class
AfricaShopItemSpider
implements
IItemSpider
{
public
class
AfricaShopItemSpider
implements
IItemSpider
{
...
@@ -37,50 +39,167 @@ public class AfricaShopItemSpider implements IItemSpider {
...
@@ -37,50 +39,167 @@ public class AfricaShopItemSpider implements IItemSpider {
//获取商品相关信息,详情放在<script> 标签里 <script type="application/json" id="ProductJson-product-template">
//获取商品相关信息,详情放在<script> 标签里 <script type="application/json" id="ProductJson-product-template">
resultObj
=
JsoupUtil
.
getScriptContentById
(
content
,
"ProductJson-product-template"
);
resultObj
=
JsoupUtil
.
getScriptContentById
(
content
,
"ProductJson-product-template"
);
//格式化为封装数据
//格式化为封装数据
ProductResponse
productResponse
=
formatProductResponse
(
resultObj
);
ProductResponse
productResponse
=
formatProductResponse
2
(
resultObj
);
resultObj
=
JSONObject
.
fromObject
(
productResponse
);
resultObj
=
JSONObject
.
fromObject
(
productResponse
);
//翻译
//翻译
TranslateHelper
.
translateProductResponse
(
resultObj
);
TranslateHelper
.
translateProductResponse
(
resultObj
);
return
resultObj
;
return
resultObj
;
}
}
/**
* 格式化 afric-eshop 返回数据
* @param resultObj
* @return
*/
private
ProductResponse
formatProductResponse2
(
JSONObject
resultObj
)
{
System
.
err
.
println
(
resultObj
);
// 声明封装类
ProductResponse
productResponse
=
new
ProductResponse
();
// 含有商品的属性,设置为true
productResponse
.
setPropFlag
(
true
);
// 无库存信息
productResponse
.
setStockFlag
(
false
);
// 库存信息,如果没有可使用的库存信息则默认为999
DynStock
dynStock
=
new
DynStock
();
dynStock
.
setSellableQuantity
(
9999
);
List
<
ProductSkuStock
>
productSkuStockList
=
dynStock
.
getProductSkuStockList
();
// 产品的原始价与优惠价
List
<
OriginalPrice
>
originalPriceList
=
new
ArrayList
<>();
List
<
ProductPromotion
>
promotionList
=
new
ArrayList
<>();
// 商品的属性,常用的商品属性为颜色与尺码
Map
<
String
,
Set
<
ProductProp
>>
productPropSet
=
new
HashMap
<>(
16
);
Set
<
ProductProp
>
propSet1
=
new
HashSet
<>(
16
);
Set
<
ProductProp
>
propSet2
=
new
HashSet
<>(
16
);
// 商品的基本属性
ItemInfo
itemInfo
=
new
ItemInfo
();
//////////////////////////////////// 获取商品基本信息 ////////////////////////////
itemInfo
.
setItemId
(
resultObj
.
getString
(
"id"
));
//取第一张
itemInfo
.
setShopName
(
PlatformEnum
.
AfriEshop
.
getValue
());
itemInfo
.
setShopUrl
(
"https://www.afrieshop.com/"
);
itemInfo
.
setTitle
(
resultObj
.
getString
(
"title"
));
//////////////////////////////////// 获取商品基本信息End /////////////////////////
// 该商品拥有的属性
JSONArray
optionsArray
=
resultObj
.
getJSONArray
(
"options"
);
// 商品列表
JSONArray
variantsArray
=
resultObj
.
getJSONArray
(
"variants"
);
List
<
String
>
options
=
new
ArrayList
<>(
3
);
// 获取所有的属性名
for
(
int
i
=
0
;
i
<
optionsArray
.
size
();
i
++)
{
options
.
add
(
optionsArray
.
getString
(
i
));
}
for
(
int
i
=
0
;
i
<
variantsArray
.
size
();
i
++)
{
JSONObject
variantsObj
=
variantsArray
.
getJSONObject
(
i
);
String
title
=
variantsObj
.
getString
(
"title"
);
String
[]
optionStr
=
title
.
split
(
"/"
);
for
(
int
j
=
0
;
j
<
options
.
size
();
j
++)
{
String
optionName
=
options
.
get
(
j
);
// 商品的属性名
String
option
=
optionStr
[
j
].
trim
();
// 商品的属性值
//商品属性
Set
<
ProductProp
>
propSet
=
new
HashSet
<>();
ProductProp
productProp
=
new
ProductProp
();
if
(
variantsObj
.
containsKey
(
"featured_image"
)
&&
!
"null"
.
equals
(
variantsObj
.
getString
(
"featured_image"
)))
{
productProp
.
setImage
(
variantsObj
.
getJSONObject
(
"featured_image"
).
getString
(
"src"
));
}
else
{
productProp
.
setImage
(
resultObj
.
getString
(
"featured_image"
));
}
productProp
.
setPropId
(
option
);
productProp
.
setPropName
(
option
);
propSet
.
add
(
productProp
);
if
(
productPropSet
.
get
(
optionName
)
==
null
)
{
productPropSet
.
put
(
optionName
,
propSet
);
}
else
{
Set
<
ProductProp
>
oldPropSet
=
productPropSet
.
get
(
optionName
);
propSet
.
addAll
(
oldPropSet
);
productPropSet
.
put
(
optionName
,
propSet
);
}
}
String
skuStr
=
""
;
if
(
optionStr
.
length
>
1
)
{
skuStr
=
";"
+
optionStr
[
0
]
+
";"
+
optionStr
[
1
]
+
";"
;
}
else
{
skuStr
=
";"
+
optionStr
[
0
]
+
";"
;
}
///////////////////原始价////////////////////////////////////
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////////////////////////////////
////////////////////////////////////获取库存 ////////////////
// 设置:商品包含库存信息
if
(
productSkuStockList
==
null
)
{
productSkuStockList
=
new
ArrayList
<>();
}
ProductSkuStock
productSkuStock
=
new
ProductSkuStock
();
productSkuStock
.
setSellableQuantity
(
999
);
productSkuStock
.
setSkuStr
(
skuStr
);
productSkuStockList
.
add
(
productSkuStock
);
dynStock
.
setProductSkuStockList
(
productSkuStockList
);
////////////////////////////////////获取库存 END/////////////
}
productResponse
.
setProductPropSet
(
productPropSet
);
productResponse
.
setPlatform
(
"Afrieshop"
);
productResponse
.
setPromotionList
(
promotionList
);
productResponse
.
setOriginalPriceList
(
originalPriceList
);
productResponse
.
setItemInfo
(
itemInfo
);
productResponse
.
setDynStock
(
dynStock
);
return
productResponse
;
}
/**
/**
* 格式化 afric-eshop 返回数据
* 格式化 afric-eshop 返回数据
*
*
* @param resultObj
* @param resultObj
* @return
* @return
*/
*/
@Deprecated
private
ProductResponse
formatProductResponse
(
JSONObject
resultObj
)
{
private
ProductResponse
formatProductResponse
(
JSONObject
resultObj
)
{
System
.
err
.
println
(
resultObj
);
ProductResponse
productResponse
=
new
ProductResponse
();
ProductResponse
productResponse
=
new
ProductResponse
();
//原始价
//
原始价
List
<
OriginalPrice
>
originalPriceList
=
new
ArrayList
<>();
List
<
OriginalPrice
>
originalPriceList
=
new
ArrayList
<>();
//促销价格
//
促销价格
List
<
ProductPromotion
>
promotionList
=
new
ArrayList
<>();
List
<
ProductPromotion
>
promotionList
=
new
ArrayList
<>();
//库存
//
库存
DynStock
dynStock
=
new
DynStock
();
DynStock
dynStock
=
new
DynStock
();
//其实数据没有包含确切的库存数,这里默认给足量的库存
//
其实数据没有包含确切的库存数,这里默认给足量的库存
dynStock
.
setSellableQuantity
(
9999
);
dynStock
.
setSellableQuantity
(
9999
);
//nike 基本是 颜色、尺码属性
//
nike 基本是 颜色、尺码属性
Map
<
String
,
Set
<
ProductProp
>>
productPropSet
=
new
HashMap
<>();
Map
<
String
,
Set
<
ProductProp
>>
productPropSet
=
new
HashMap
<>();
//商品基本信息
//
商品基本信息
ItemInfo
itemInfo
=
new
ItemInfo
();
ItemInfo
itemInfo
=
new
ItemInfo
();
JSONArray
variantsArray
=
resultObj
.
getJSONArray
(
"variants"
);
JSONArray
variantsArray
=
resultObj
.
getJSONArray
(
"variants"
);
//属性
//
属性
JSONArray
optionsArray
=
resultObj
.
getJSONArray
(
"options"
);
JSONArray
optionsArray
=
resultObj
.
getJSONArray
(
"options"
);
for
(
int
i
=
0
;
i
<
variantsArray
.
size
();
i
++)
{
for
(
int
i
=
0
;
i
<
variantsArray
.
size
();
i
++)
{
//属性
//
属性
JSONArray
itemOptionsArray
=
variantsArray
.
getJSONObject
(
i
).
getJSONArray
(
"options"
);
JSONArray
itemOptionsArray
=
variantsArray
.
getJSONObject
(
i
).
getJSONArray
(
"options"
);
String
skuStr
=
";"
;
String
skuStr
=
";"
;
for
(
int
m
=
0
;
m
<
itemOptionsArray
.
size
();
m
++)
{
for
(
int
m
=
0
;
m
<
itemOptionsArray
.
size
();
m
++)
{
skuStr
=
skuStr
+
KeyConstant
.
CUSTOMIZE_ID
+
itemOptionsArray
.
getString
(
m
)
+
";"
;
skuStr
=
skuStr
+
KeyConstant
.
CUSTOMIZE_ID
+
itemOptionsArray
.
getString
(
m
)
+
";"
;
}
}
//没有属性的时候,会返回 Default Title
if
(
"Default Title"
.
equalsIgnoreCase
(
itemOptionsArray
.
getString
(
0
)))
{
}
///////////////////原始价////////////////////////////////////
///////////////////原始价////////////////////////////////////
OriginalPrice
originalPrice
=
new
OriginalPrice
();
OriginalPrice
originalPrice
=
new
OriginalPrice
();
String
price
=
variantsArray
.
getJSONObject
(
i
).
getString
(
"price"
);
String
price
=
variantsArray
.
getJSONObject
(
i
).
getString
(
"price"
);
...
@@ -91,7 +210,8 @@ public class AfricaShopItemSpider implements IItemSpider {
...
@@ -91,7 +210,8 @@ public class AfricaShopItemSpider implements IItemSpider {
originalPrice
.
setSkuStr
(
skuStr
);
originalPrice
.
setSkuStr
(
skuStr
);
originalPriceList
.
add
(
originalPrice
);
originalPriceList
.
add
(
originalPrice
);
///////////////////原始价 END////////////////////////////////
///////////////////原始价 END////////////////////////////////
////////////////////////////////////获取库存 ////////////////////////////////////////////
////////////////////////////////////获取库存 ////////////////
productResponse
.
setStockFlag
(
true
);
productResponse
.
setStockFlag
(
true
);
List
<
ProductSkuStock
>
productSkuStockList
=
dynStock
.
getProductSkuStockList
();
List
<
ProductSkuStock
>
productSkuStockList
=
dynStock
.
getProductSkuStockList
();
if
(
productSkuStockList
==
null
)
{
if
(
productSkuStockList
==
null
)
{
...
@@ -103,10 +223,11 @@ public class AfricaShopItemSpider implements IItemSpider {
...
@@ -103,10 +223,11 @@ public class AfricaShopItemSpider implements IItemSpider {
productSkuStock
.
setSkuStr
(
skuStr
);
productSkuStock
.
setSkuStr
(
skuStr
);
productSkuStockList
.
add
(
productSkuStock
);
productSkuStockList
.
add
(
productSkuStock
);
dynStock
.
setProductSkuStockList
(
productSkuStockList
);
dynStock
.
setProductSkuStockList
(
productSkuStockList
);
////////////////////////////////////获取库存 END/////////////
///////////////////////////////
////////////////////////////////////获取库存 END/////////////
//获取所有的属性
//获取所有的属性
for
(
int
j
=
0
;
j
<
optionsArray
.
size
();
j
++)
{
for
(
int
j
=
0
;
j
<
optionsArray
.
size
();
j
++)
{
////////////////////////////////////获取商品属性////////////////////////////////////////////
////////////////////////////////////获取商品属性/////////
//商品属性
//商品属性
Set
<
ProductProp
>
propSet
=
new
HashSet
<>();
Set
<
ProductProp
>
propSet
=
new
HashSet
<>();
ProductProp
productProp
=
new
ProductProp
();
ProductProp
productProp
=
new
ProductProp
();
...
@@ -120,10 +241,10 @@ public class AfricaShopItemSpider implements IItemSpider {
...
@@ -120,10 +241,10 @@ public class AfricaShopItemSpider implements IItemSpider {
propSet
.
addAll
(
oldPropSet
);
propSet
.
addAll
(
oldPropSet
);
productPropSet
.
put
(
optionsArray
.
getString
(
j
),
propSet
);
productPropSet
.
put
(
optionsArray
.
getString
(
j
),
propSet
);
}
}
////////////////////////////////////获取属性 END/////////
///////////////////////////////////
////////////////////////////////////获取属性 END/////////
}
}
}
}
itemInfo
.
setItemId
(
resultObj
.
getString
(
"id"
));
itemInfo
.
setItemId
(
resultObj
.
getString
(
"id"
));
//取第一张
//取第一张
itemInfo
.
setPic
(
resultObj
.
getString
(
"featured_image"
));
itemInfo
.
setPic
(
resultObj
.
getString
(
"featured_image"
));
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论