Finish sku_crawl
This commit is contained in:
@@ -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());
|
||||
}
|
||||
@@ -354,24 +312,201 @@ class BrandSeriesService
|
||||
return $msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 抓取车型数据(通过接口)
|
||||
* @param Request $request
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function crawlKeyword(Request $request): array
|
||||
{
|
||||
$keyword = $request->get('keyword');
|
||||
|
||||
// 执行抓取
|
||||
$crawlService = new CrawlService();
|
||||
|
||||
//懂车帝模糊搜索返回车系地址
|
||||
$href = $crawlService->modelSearch($keyword);
|
||||
$seriesId = $this->extractSeriesId($href);
|
||||
|
||||
if (empty($seriesId)) {
|
||||
throw new \Exception('抓取失败,未找到该系列车型');
|
||||
}
|
||||
|
||||
//抓取品牌与系列
|
||||
$brandSeries = $crawlService->crawlBrandSeries($seriesId);
|
||||
|
||||
|
||||
// 验证品牌是否存在
|
||||
$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];
|
||||
if($brand){
|
||||
$result['brand']['id'] = $brand['id'];
|
||||
}
|
||||
$result['series'] = ['series_name' => $brandSeries['series'], 'exists' => $series?1:0];
|
||||
if($series){
|
||||
$result['series']['id'] = $series['id'];
|
||||
if(empty($result['brand']['id'])){
|
||||
$result['brand']['id'] = $series['brand_id'];
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
//获取车型数据
|
||||
$result['model'] = $this->crawlSeriesModel($seriesId);
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从车型名中提取年份
|
||||
* @param string $carModel 车型名称
|
||||
* @return string|null 提取到的年份或null
|
||||
* @param Request $request
|
||||
* @return array|string
|
||||
* @throws \Exception
|
||||
*/
|
||||
function extractYearFromCarModel($carModel)
|
||||
public function crawlKeywordCreate(Request $request)
|
||||
{
|
||||
// 匹配4位数字 + "款" 的模式
|
||||
if (preg_match('/(\d{4})款/', $carModel, $matches)) {
|
||||
return $matches[1];
|
||||
$series_id = $request->input('series_id');
|
||||
$brand_id = $request->input('brand_id');
|
||||
$series_name = $request->input('series_name');
|
||||
$models = $request->input('models');
|
||||
|
||||
if(empty($brand_id)){
|
||||
throw new \Exception("请选择品牌");
|
||||
}
|
||||
if(empty($series_id) && empty($series_name)){
|
||||
throw new \Exception("请选择车系");
|
||||
}
|
||||
|
||||
// 匹配4位数字 + 空格或结尾的模式
|
||||
if (preg_match('/^(\d{4})(?=\s|$)/', $carModel, $matches)) {
|
||||
return $matches[1];
|
||||
if (empty($series_id)) {
|
||||
$series = BrandSeries::where('brand_id', $brand_id)->where('series_name', $series_name)->first();
|
||||
if (empty($brandseries)) { //如果没有该车系则创建
|
||||
$seriesService = new \app\admin\service\BrandSeriesService();
|
||||
$seriesData = [
|
||||
'brand_id' => $brand_id,
|
||||
'series_code' => $seriesService->generateUniqueModificationCode($brand_id),
|
||||
'series_name' => $series_name,
|
||||
];
|
||||
$series = CrudUtil::create(BrandSeries::class, $seriesData);
|
||||
}
|
||||
$series_id = $series->id;
|
||||
}else{
|
||||
$series = BrandSeries::where('id', $series_id)->first();
|
||||
}
|
||||
// 获取品牌信息
|
||||
$brand = Brand::where('id', $series->brand_id)->first();
|
||||
|
||||
// 保存车型数据
|
||||
$createdCount = 0;
|
||||
$repeat = 0;
|
||||
$fail = 0;
|
||||
$msg = '';
|
||||
|
||||
foreach ($models as $item) {
|
||||
$modelData = [
|
||||
'brand_code' => $brand->brand_code,
|
||||
'brand_name' => $brand->brand_name,
|
||||
'series_code' => $series->series_code,
|
||||
'series_name' => $series->series_name,
|
||||
'model_name' => $item['title'],
|
||||
'model_year' => $item['year'],
|
||||
'msrp' => $item['msrp'],
|
||||
];
|
||||
//指纹数组
|
||||
$fingerprintArr = [
|
||||
'brand_name' => $modelData['brand_name'],
|
||||
'series_name' => $modelData['series_name'],
|
||||
'model_name' => $modelData['model_name'],
|
||||
'model_year' => $modelData['model_year'],
|
||||
'msrp' => $modelData['msrp'],
|
||||
];
|
||||
// 生成指纹
|
||||
$modelData['fingerprint'] = Fingerprint::makeFromArray($fingerprintArr);
|
||||
|
||||
$modelData['model_code'] = ModelNaming::buildModelCode($modelData);
|
||||
$modelData['sku_code'] = SkuCodeBuilder::build($modelData);
|
||||
|
||||
try {
|
||||
$model = VehVehicleSku::where('brand_name', $brand->brand_name)->where('series_name', $series->series_name)->where('model_name', $item['title'])->first();
|
||||
if (!empty($model)) {
|
||||
$repeat++;
|
||||
continue;
|
||||
}
|
||||
CrudUtil::create(VehVehicleSku::class, $modelData);
|
||||
$createdCount++;
|
||||
} catch (\Exception $e) {
|
||||
// 记录错误但继续处理其他车型
|
||||
$msg .= "创建车型失败: " . $e->getMessage() . "\n";
|
||||
Log::error("创建车型失败:" . $msg, ['series_id' => $series_id, 'model' => $item]);
|
||||
$fail++;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
Log::info('series-crawl-' . $series_id . ' ' . $msg);
|
||||
$msg = '创建车型成功' . $createdCount . '条';
|
||||
$msg .= (!empty($repeat)) ? ',重复' . $repeat . '条' : '';
|
||||
$msg .= (!empty($fail)) ? ',失败' . $fail . '条' : '';
|
||||
return $msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取车系下车型数据
|
||||
* @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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,40 @@ 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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存抓取的系列车型
|
||||
* @param Request $request
|
||||
* @return \support\Response
|
||||
*/
|
||||
public function crawlKeywordCreate(Request $request){
|
||||
try {
|
||||
$result = $this->service->crawlKeywordCreate($request);
|
||||
if($result){
|
||||
return $this->success($result,'成功');
|
||||
}else{
|
||||
return $this->error('创建失败');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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]];
|
||||
}
|
||||
}
|
||||
@@ -109,6 +109,8 @@ 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']);
|
||||
Route::any('/crawlKeywordCreate', [app\common\controller\BrandSeriesController::class, 'crawlKeywordCreate']);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user