关键词抓取
This commit is contained in:
@@ -330,35 +330,59 @@ class BrandSeriesService
|
|||||||
|
|
||||||
//懂车帝模糊搜索返回车系地址
|
//懂车帝模糊搜索返回车系地址
|
||||||
$href = $crawlService->modelSearch($keyword);
|
$href = $crawlService->modelSearch($keyword);
|
||||||
$seriesId = $this->extractSeriesId($href);
|
$seriesId = $this->extractSeriesId($href); //懂车帝车系id
|
||||||
|
|
||||||
if (empty($seriesId)) {
|
if (empty($seriesId)) { //没有抓取到懂车帝系列id时,按关键词为品牌名抓取所有的系列名
|
||||||
|
$crawlSeries = $crawlService->brandSeries($keyword);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($seriesId) && empty($crawlSeries)) {
|
||||||
throw new \Exception('抓取失败,未找到该系列车型');
|
throw new \Exception('抓取失败,未找到该系列车型');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$brandSerieses = [];
|
||||||
|
if(empty($seriesId) && !empty($crawlSeries)){ //关键词为品牌名时抓取所有车系的懂车帝id
|
||||||
|
$href = $crawlService->modelSearch($crawlSeries[0]);
|
||||||
|
$seriesId = $this->extractSeriesId($href); //懂车帝车系id
|
||||||
//抓取品牌与系列
|
//抓取品牌与系列
|
||||||
$brandSeries = $crawlService->crawlBrandSeries($seriesId);
|
$brandSeries = $crawlService->crawlBrandSeries($seriesId);
|
||||||
|
foreach ($crawlSeries as $item) {
|
||||||
|
// $brandSerieses[] = $crawlService->brandSeries($keyword);
|
||||||
|
$brandSerieses[] = ['brand' => $brandSeries['brand'], 'series' => $item];
|
||||||
|
}
|
||||||
|
}elseif (!empty($seriesId) && empty($crawlSeries)){
|
||||||
|
//抓取品牌与系列
|
||||||
|
$brandSerieses[] = $crawlService->crawlBrandSeries($seriesId);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = [];
|
||||||
|
try {
|
||||||
|
foreach ($brandSerieses as $brandSeries) {
|
||||||
|
$item = [];
|
||||||
// 验证品牌是否存在
|
// 验证品牌是否存在
|
||||||
$brand = Brand::where('brand_name', $brandSeries['brand'])->first();
|
$brand = Brand::where('brand_name', $brandSeries['brand'])->first();
|
||||||
// 获取品牌信息
|
// 获取品牌信息
|
||||||
$series = BrandSeries::where('series_name', $brandSeries['series'])->first();
|
$series = BrandSeries::where('series_name', $brandSeries['series'])->first();
|
||||||
$result['brand'] = ['brand_name' => $brandSeries['brand'], 'exists' => $brand?1:0];
|
$item['brand'] = ['brand_name' => $brandSeries['brand'], 'exists' => $brand ? 1 : 0];
|
||||||
if($brand){
|
if ($brand) {
|
||||||
$result['brand']['id'] = $brand['id'];
|
$item['brand']['id'] = $brand['id'];
|
||||||
}
|
}
|
||||||
$result['series'] = ['series_name' => $brandSeries['series'], 'exists' => $series?1:0];
|
$item['series'] = ['series_name' => $brandSeries['series'], 'exists' => $series ? 1 : 0];
|
||||||
if($series){
|
if ($series) {
|
||||||
$result['series']['id'] = $series['id'];
|
$item['series']['id'] = $series['id'];
|
||||||
if(empty($result['brand']['id'])){
|
if (empty($item['brand']['id'])) {
|
||||||
$result['brand']['id'] = $series['brand_id'];
|
$item['brand']['id'] = $series['brand_id'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
//获取车型数据
|
//获取车型数据
|
||||||
$result['model'] = $this->crawlSeriesModel($seriesId);
|
$item['model'] = [];
|
||||||
|
if (count($brandSerieses)<2) {
|
||||||
|
$item['model'] = $this->crawlSeriesModel($seriesId);
|
||||||
|
}
|
||||||
|
$result[] = $item;
|
||||||
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
throw new \Exception($e->getMessage());
|
throw new \Exception($e->getMessage());
|
||||||
|
|||||||
Reference in New Issue
Block a user