抓取车型信息

This commit is contained in:
2026-02-04 17:35:08 +08:00
parent 2da3804953
commit eee09316df
4 changed files with 177 additions and 5 deletions
+106 -5
View File
@@ -398,21 +398,39 @@ class BrandSeriesService
*/ */
public function crawlKeywordCreate(Request $request) public function crawlKeywordCreate(Request $request)
{ {
$series_id = $request->input('series_id');
$brand_id = $request->input('brand_id'); $brand_id = $request->input('brand_id');
$brand_name = $request->input('brand_name');
$brand_code = $request->input('brand_code');
$series_id = $request->input('series_id');
$series_name = $request->input('series_name'); $series_name = $request->input('series_name');
$models = $request->input('models'); $models = $request->input('models');
if(empty($brand_id)){ if(empty($brand_id) && (empty($brand_name) || empty($brand_code))){
throw new \Exception("请选择品牌"); throw new \Exception("请选择品牌");
} }
if(empty($series_id) && empty($series_name)){ if(empty($series_id) && empty($series_name)){
throw new \Exception("请选择车系"); throw new \Exception("请选择车系");
} }
// 获取品牌信息
if (empty($brand_id)) {
$brand = Brand::where('id', $brand_id)->where('brand_name', $brand_name)->first();
if (empty($brand)) { //如果没有该车系则创建
$brandService = new \app\admin\service\BrandService();
$brandData = [
'brand_code' => $brandService->generateUniqueCode(),
'brand_name' => $brand_name,
];
$brand = CrudUtil::create(Brand::class, $brandData);
}
$brand_id = $brand->id;
}else{
$brand = Brand::where('id', $brand_id)->first();
}
if (empty($series_id)) { if (empty($series_id)) {
$series = BrandSeries::where('brand_id', $brand_id)->where('series_name', $series_name)->first(); $series = BrandSeries::where('brand_id', $brand_id)->where('series_name', $series_name)->first();
if (empty($brandseries)) { //如果没有该车系则创建 if (empty($series)) { //如果没有该车系则创建
$seriesService = new \app\admin\service\BrandSeriesService(); $seriesService = new \app\admin\service\BrandSeriesService();
$seriesData = [ $seriesData = [
'brand_id' => $brand_id, 'brand_id' => $brand_id,
@@ -425,8 +443,6 @@ class BrandSeriesService
}else{ }else{
$series = BrandSeries::where('id', $series_id)->first(); $series = BrandSeries::where('id', $series_id)->first();
} }
// 获取品牌信息
$brand = Brand::where('id', $series->brand_id)->first();
// 保存车型数据 // 保存车型数据
$createdCount = 0; $createdCount = 0;
@@ -481,6 +497,91 @@ class BrandSeriesService
return $msg; return $msg;
} }
/**
* 抓取
*/
public function crawl(Request $request): array
{
$keyword = $request->get('keyword');
if(filter_var($keyword, FILTER_VALIDATE_URL)){
return $this->crawlUrl($request);
}else{
return $this->crawlKeyword($request);
}
}
/**
* 抓取车型数据(通过接口)
* @param Request $request
* @return array
* @throws \Exception
*/
public function crawlUrl(Request $request): array
{
//$url = $request->get('url');
$url = $request->get('keyword');
// 执行抓取
$crawlService = new CrawlService();
//懂车帝提取车系id
$seriesId = $this->get_series_from_url($url); //懂车帝车系id
if (empty($seriesId)) {
throw new \Exception('抓取失败,未找到该系列车型');
}
$brandSerieses = [];
//抓取品牌与系列
$brandSeries = $crawlService->crawlBrandSeries($seriesId);
$brandSerieses[] = ['brand' => $brandSeries['brand'], 'series' => $brandSeries['series']];
$result = [];
try {
foreach ($brandSerieses as $brandSeries) {
$item = [];
// 验证品牌是否存在
$brand = Brand::where('brand_name', $brandSeries['brand'])->first();
// 获取品牌信息
$series = BrandSeries::where('series_name', $brandSeries['series'])->first();
$item['brand'] = ['brand_name' => $brandSeries['brand'], 'exists' => $brand ? 1 : 0];
if ($brand) {
$item['brand']['id'] = $brand['id'];
}
$item['series'] = ['series_name' => $brandSeries['series'], 'exists' => $series ? 1 : 0];
if ($series) {
$item['series']['id'] = $series['id'];
if (empty($item['brand']['id'])) {
$item['brand']['id'] = $series['brand_id'];
}
}
//获取车型数据
$item['model'] = [];
if (count($brandSerieses)<2) {
$item['model'] = $this->crawlSeriesModel($seriesId);
}
$result[] = $item;
}
return ['is_series' => (count($brandSerieses)<2)?1:0, 'data' => $result];
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
public function get_series_from_url($url) {
// 使用正则表达式匹配 "https://www.dongchedi.com/auto/series/{数字}"
$pattern = '/^https:\/\/www\.dongchedi\.com\/auto\/series\/(\d+)$/';
if (preg_match($pattern, $url, $matches)) {
// 提取数字部分作为 series ID
$series_id = $matches[1];
return $series_id; // 返回系列ID
} else {
throw new \Exception("URL格式不匹配");
}
}
/** /**
* 获取车系下车型数据 * 获取车系下车型数据
* @return array * @return array
+33
View File
@@ -151,4 +151,37 @@ class BrandService
$msg .= (!empty($fail))?',失败' . $fail . '条':''; $msg .= (!empty($fail))?',失败' . $fail . '条':'';
return $msg; return $msg;
} }
/**
* 生成唯一的7位颜色编码
* 格式:C + (O/I) + 5位字母数字组合
*
* @param int $type 类型
* @return string 7位唯一编码
*/
public function generateUniqueCode()
{
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$maxAttempts = 10; // 防止无限循环
for ($attempt = 0; $attempt < $maxAttempts; $attempt++) {
// 生成后5位随机字符
$suffix = '';
for ($i = 0; $i < 8; $i++) {
$suffix .= $chars[rand(0, strlen($chars) - 1)];
}
$code = $suffix;
// 检查是否已存在
$existing = Brand::where('brand_code', $code)->first();
if (!$existing) {
return $code;
}
}
// 如果1000次尝试后仍然重复,使用时间戳确保唯一性
$timestamp = substr(time(), -6);
return $suffix . $timestamp;
}
} }
@@ -101,6 +101,42 @@ class BrandSeriesController extends BaseController
} }
} }
/**
* URL抓取品牌、车系、车型
* @param Request $request
* @return \support\Response
*/
public function crawlUrl(Request $request){
try {
$result = $this->service->crawlUrl($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 crawl(Request $request){
try {
$result = $this->service->crawl($request);
if($result){
return $this->success($result,'抓取成功');
}else{
return $this->error('抓取任务失败');
}
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
}
/** /**
* 保存抓取的系列车型 * 保存抓取的系列车型
* @param Request $request * @param Request $request
+2
View File
@@ -116,6 +116,8 @@ Route::group('/common', function () {
Route::any('/crawlModelCreate', [app\common\controller\BrandSeriesController::class, 'crawlModelCreate']); Route::any('/crawlModelCreate', [app\common\controller\BrandSeriesController::class, 'crawlModelCreate']);
Route::any('/crawlKeyword', [app\common\controller\BrandSeriesController::class, 'crawlKeyword']); Route::any('/crawlKeyword', [app\common\controller\BrandSeriesController::class, 'crawlKeyword']);
Route::any('/crawlKeywordCreate', [app\common\controller\BrandSeriesController::class, 'crawlKeywordCreate']); Route::any('/crawlKeywordCreate', [app\common\controller\BrandSeriesController::class, 'crawlKeywordCreate']);
Route::any('/crawlUrl', [app\common\controller\BrandSeriesController::class, 'crawlUrl']);
Route::any('/crawl', [app\common\controller\BrandSeriesController::class, 'crawl']);
}); });
}); });