diff --git a/app/admin/service/BrandSeriesService.php b/app/admin/service/BrandSeriesService.php index f1526d3..0cf4901 100644 --- a/app/admin/service/BrandSeriesService.php +++ b/app/admin/service/BrandSeriesService.php @@ -168,50 +168,8 @@ class BrandSeriesService throw new \Exception('抓取失败,未找到该系列车型'); } - $client = new Client([ - 'verify' => false, // 禁用 SSL 验证 - 'timeout' => 10 - ]); try { - $response = $client->get('https://www.dongchedi.com/motor/pc/car/series/car_list', [ - 'query' => ['series_id' => $seriesId], - 'headers' => [ - 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' - ] - ]); - $data = json_decode($response->getBody()->getContents(), true); - if($data['status'] != 0 || (!isset($data['data']) && !isset($data['data']['tab_list']))){ - Log::error('抓取车型数据失败' . $data['message'], $data); - throw new \Exception('抓取车型数据失败' . $data['message']); - } - $models = []; - foreach ($data['data']['tab_list'] as $item) { - if($item['tab_text'] == '在售' || $item['tab_text'] == '停售'){ - unset($item['data'][0]); - $models = array_merge($models, $item['data']); - } - } - foreach ($models as $key => $model) { - if(!isset($model["info"]['car_name']) || $model['type'] != "1115"){ - unset($models[$key]); - continue; - } - $official_price = 0; - if(!isset($model["info"]['official_price'])){ - $official_price = (int)(str_replace("万", "", $model["info"]['official_price_str']) * 10000) ?? 0; - }else{ - $official_price = $model["info"]['official_price'] * 10000; - } - $models[$key] = [ - 'title' => $model["info"]['year'] . '款 ' .$model["info"]['car_name'], - 'brand_name' => $model["info"]['brand_name'], - 'series_name' => $model["info"]['series_name'], - 'year' => $model["info"]['year'], - 'msrp' => $official_price, - 'car_id' => $model["info"]['id'], - ]; - } - return array_values($models); + return $this->crawlSeriesModel($seriesId); } catch (\Exception $e) { throw new \Exception($e->getMessage()); } @@ -356,22 +314,101 @@ class BrandSeriesService /** - * 从车型名中提取年份 - * @param string $carModel 车型名称 - * @return string|null 提取到的年份或null + * 抓取车型数据(通过接口) + * @param Request $request + * @return array + * @throws \Exception */ - function extractYearFromCarModel($carModel) + public function crawlKeyword(Request $request): array { - // 匹配4位数字 + "款" 的模式 - if (preg_match('/(\d{4})款/', $carModel, $matches)) { - return $matches[1]; + $keyword = $request->get('keyword'); + + // 执行抓取 + $crawlService = new CrawlService(); + + //懂车帝模糊搜索返回车系地址 + $href = $crawlService->modelSearch($keyword); + $seriesId = $this->extractSeriesId($href); + + if (empty($seriesId)) { + throw new \Exception('抓取失败,未找到该系列车型'); } - // 匹配4位数字 + 空格或结尾的模式 - if (preg_match('/^(\d{4})(?=\s|$)/', $carModel, $matches)) { - return $matches[1]; - } + //抓取品牌与系列 + $brandSeries = $crawlService->crawlBrandSeries($seriesId); - return null; + + // 验证品牌是否存在 + $brand = Brand::where('brand_name', $brandSeries['brand'])->first(); + // 获取品牌信息 + $series = BrandSeries::where('series_name', $brandSeries['series'])->first(); + $result['brand'] = ['brand_name' => $brandSeries['brand'], 'exists' => $brand?1:0]; + $result['series'] = ['series_name' => $brandSeries['series'], 'exists' => $series?1:0]; + + try { + //获取车型数据 + $result['model'] = $this->crawlSeriesModel($seriesId); + return $result; + } catch (\Exception $e) { + throw new \Exception($e->getMessage()); + } + } + + /** + * 获取车系下车型数据 + * @return array + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function crawlSeriesModel($seriesId) + { + $client = new Client([ + 'verify' => false, // 禁用 SSL 验证 + 'timeout' => 10 + ]); + try { + $response = $client->get('https://www.dongchedi.com/motor/pc/car/series/car_list', [ + 'query' => ['series_id' => $seriesId], + 'headers' => [ + 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' + ] + ]); + $data = json_decode($response->getBody()->getContents(), true); + if($data['status'] != 0 || (!isset($data['data']) && !isset($data['data']['tab_list']))){ + Log::error('抓取车型数据失败' . $data['message'], $data); + throw new \Exception('抓取车型数据失败' . $data['message']); + } + $models = []; + foreach ($data['data']['tab_list'] as $item) { + if($item['tab_text'] == '在售' || $item['tab_text'] == '停售'){ + unset($item['data'][0]); + $models = array_merge($models, $item['data']); + } + } + foreach ($models as $key => $model) { + if(!isset($model["info"]['car_name']) || $model['type'] != "1115"){ + unset($models[$key]); + continue; + } + $official_price = 0; + if(!isset($model["info"]['official_price'])){ + $official_price = (int)(str_replace("万", "", $model["info"]['official_price_str']) * 10000) ?? 0; + }else{ + $official_price = $model["info"]['official_price'] * 10000; + } + $models[$key] = [ + 'title' => $model["info"]['year'] . '款 ' .$model["info"]['car_name'], + 'brand_name' => $model["info"]['brand_name'], + 'series_name' => $model["info"]['series_name'], + 'year' => $model["info"]['year'], + 'msrp' => $official_price, + 'car_id' => $model["info"]['id'], + ]; + } + + return array_values($models); + + } catch (\Exception $e) { + throw new \Exception($e->getMessage()); + } } } \ No newline at end of file diff --git a/app/common/controller/BrandSeriesController.php b/app/common/controller/BrandSeriesController.php index 00faa00..61c1b37 100644 --- a/app/common/controller/BrandSeriesController.php +++ b/app/common/controller/BrandSeriesController.php @@ -48,7 +48,7 @@ class BrandSeriesController extends BaseController } /** - * 异步抓取系列车型 + * 抓取系列车型 * @param Request $request * @return \support\Response */ @@ -58,7 +58,7 @@ class BrandSeriesController extends BaseController if($result){ return $this->success($result,'抓取成功'); }else{ - return $this->error('抓取任务提交失败'); + return $this->error('抓取任务失败'); } } catch (\Exception $e) { return $this->error($e->getMessage()); @@ -66,7 +66,7 @@ class BrandSeriesController extends BaseController } /** - * 异步抓取系列车型 + * 保存抓取的系列车型 * @param Request $request * @return \support\Response */ @@ -82,4 +82,23 @@ class BrandSeriesController extends BaseController return $this->error($e->getMessage()); } } + + + /** + * 关键词抓取品牌、车系、车型 + * @param Request $request + * @return \support\Response + */ + public function crawlKeyword(Request $request){ + try { + $result = $this->service->crawlKeyword($request); + if($result){ + return $this->success($result,'抓取成功'); + }else{ + return $this->error('抓取任务失败'); + } + } catch (\Exception $e) { + return $this->error($e->getMessage()); + } + } } \ No newline at end of file diff --git a/app/common/service/CrawlService.php b/app/common/service/CrawlService.php index a2c69d4..53e7026 100644 --- a/app/common/service/CrawlService.php +++ b/app/common/service/CrawlService.php @@ -73,4 +73,18 @@ class CrawlService }); return $href; } + + /** + * 关键词搜索的更多页面,通过面包屑抓取品牌与系列 + * @param $series + * @return mixed|string + */ + public function crawlBrandSeries($series) + { + $url = "https://www.dongchedi.com/auto/series/" . $series . "?anchorId=carModels"; + $ql = QueryList::get($url); + $result = $ql->find('div.breadcrumb_wrap__2GLeU span')->texts()->all(); + + return ['brand' => $result[2], 'series' => $result[3]]; + } } \ No newline at end of file diff --git a/config/route.php b/config/route.php index 2fafd16..6fa5100 100644 --- a/config/route.php +++ b/config/route.php @@ -109,6 +109,7 @@ Route::group('/common', function () { Route::any('/destroy', [app\common\controller\BrandSeriesController::class, 'destroy']); Route::any('/crawlModels', [app\common\controller\BrandSeriesController::class, 'crawlModels']); Route::any('/crawlModelCreate', [app\common\controller\BrandSeriesController::class, 'crawlModelCreate']); + Route::any('/crawlKeyword', [app\common\controller\BrandSeriesController::class, 'crawlKeyword']); }); });