diff --git a/app/common/service/UsedCarCrawlService.php b/app/common/service/UsedCarCrawlService.php index 587b03e..c28d3a7 100644 --- a/app/common/service/UsedCarCrawlService.php +++ b/app/common/service/UsedCarCrawlService.php @@ -46,7 +46,7 @@ class UsedCarCrawlService if (!$raw) { return false; } -echo '开始抓取~:'.date('Y-m-d H:i:s').PHP_EOL; + $html = $this->getHtml($raw['url']); switch ($raw['scene']) { @@ -54,15 +54,11 @@ echo '开始抓取~:'.date('Y-m-d H:i:s').PHP_EOL; $info = $this->getUsedDetail($html, $raw['url']); $id = $this->saveUsedcarBasicToDb($info); $this->iUrl($info['car_id_url'], 'config', $id); - - echo '一阶段 抓取成功~:'.date('Y-m-d H:i:s').PHP_EOL; break; case 'config': $data = $this->getConfigDetail($html); $this->saveUsedcarConfigToDb($data, $raw['id']); - - echo '二阶段 抓取成功~:'.date('Y-m-d H:i:s').PHP_EOL; break; } @@ -142,7 +138,9 @@ echo '开始抓取~:'.date('Y-m-d H:i:s').PHP_EOL; // 能源类型 $fuelForm = $jsonArray['props']['pageProps']['skuDetail']['car_config_overview']['power']['fuel_form'] ?? ''; - + $newFuelForm = $jsonArray['props']['pageProps']['skuDetail']['car_config_overview']['new_energy_power']['fuel_form'] ?? ''; + $resultFuelForm = $fuelForm === '' ? $newFuelForm : $fuelForm; + // 使用里程 $importantText = $jsonArray['props']['pageProps']['skuDetail']['important_text'] ?? ''; $importantTextArray = explode(' | ', $importantText); @@ -186,8 +184,8 @@ echo '开始抓取~:'.date('Y-m-d H:i:s').PHP_EOL; $info['car_id_url'] = $carIdUrl; $info['used_detail'] = $jsonText; $info['format_shop_info'] = json_encode($formatShopInfo); - $info['fuel_form'] = $fuelForm; - $info['classify_energy'] = $this->classifyEnergy($fuelForm); + $info['fuel_form'] = $resultFuelForm; + $info['classify_energy'] = $this->detectEnergyType($resultFuelForm); $info['usage_mileage'] = $usageMileage; $info['brand_name'] = $brandName; $info['series_name'] = $seriesName; @@ -291,54 +289,85 @@ echo '开始抓取~:'.date('Y-m-d H:i:s').PHP_EOL; } /** - * 获取能源类型 + * 根据给定字符串判断车辆能源类型 + * + * 返回: + * - new_energy 新能源(纯电、插电混动、增程、氢燃料等) + * - traditional 传统能源(汽油、柴油、天然气、非插电混动等) + * - unknown 无法判断 + * + * @param string $text 例如:'2022款 某某牌 2.0L 油电混合 双擎' + * @return string */ - public function classifyEnergy($input) + public function detectEnergyType(string $text): string { - $ce = ['汽油', '柴油', '天然气']; - $re = ['纯电', '电动', '氢能', '氢燃料', '太阳能', '油电混动', '混动', '插电混动', '插电式', '增程式', '增程']; - - // 标准化输入:去除空格等 - $input = trim($input); - - // 1. 精确或子串匹配(优先) - foreach ($re as $keyword) { - if (strpos($input, $keyword) !== false) { - return 'RE'; // 新能源 - } + if (function_exists('mb_internal_encoding')) { + mb_internal_encoding('UTF-8'); } - foreach ($ce as $keyword) { - if (strpos($input, $keyword) !== false) { - return 'CE'; // 传统能源 + + // 明确定义的新能源关键字(纯电 / 插电 / 增程 / 氢 / 太阳能 等) + $newEnergyKeywords = [ + // 纯电 + '纯电', '纯电动', '电动车', 'BEV', + // 插电式混合 + '插电混动', '插电式混合', '插电式', '插电', 'PHEV', + // 增程式 + '增程式', '增程', 'REEV', + // 氢能 / 燃料电池 + '氢能', '氢燃料', '燃料电池', 'FCEV', + // 其它 + '太阳能', + ]; + + // 传统能源关键字(包括燃油/燃气) + $traditionalKeywords = [ + '汽油', + '柴油', + '天然气', 'LNG', 'CNG', + ]; + + // 普通混动关键字(按你的要求,统一归为传统能源) + $hybridKeywords = [ + '油电混合', '油电混动', '油电', + '混动', // 不带“插电”的混动 + '双擎', // 丰田常见表达 + 'HEV', + ]; + + // 辅助:不区分大小写的包含判断,优先 mb_stripos + $contains = function (string $haystack, string $needle): bool { + if ($needle === '') { + return false; + } + if (function_exists('mb_stripos')) { + return mb_stripos($haystack, $needle) !== false; + } + return stripos($haystack, $needle) !== false; + }; + + // 1. 先判断“明确新能源”关键词(插电、增程、氢、纯电等) + foreach ($newEnergyKeywords as $kw) { + if ($contains($text, $kw)) { + return 'RE'; } } - // 2. 如果没匹配上,尝试模糊匹配(相似度 > 80%) - $bestMatch = null; - $bestScore = 0; - $bestType = null; - - // 检查 RE - foreach ($re as $keyword) { - similar_text($input, $keyword, $percent); - if ($percent > $bestScore && $percent >= 80) { - $bestScore = $percent; - $bestMatch = $keyword; - $bestType = 'RE'; + // 2. 再判断“普通油电混动”——按照你的业务,一律归为传统能源 + foreach ($hybridKeywords as $kw) { + if ($contains($text, $kw)) { + return 'CE'; } } - // 检查 CE - foreach ($ce as $keyword) { - similar_text($input, $keyword, $percent); - if ($percent > $bestScore && $percent >= 80) { - $bestScore = $percent; - $bestMatch = $keyword; - $bestType = 'CE'; + // 3. 再判断传统能源(纯燃油/气) + foreach ($traditionalKeywords as $kw) { + if ($contains($text, $kw)) { + return 'CE'; } } - return $bestType ?? 'UNKNOWN'; // 未识别 + // 4. 都没匹配上 + return 'UNKNOWN'; } /** diff --git a/app/controller/IndexController.php b/app/controller/IndexController.php index d424030..b27f703 100644 --- a/app/controller/IndexController.php +++ b/app/controller/IndexController.php @@ -161,13 +161,13 @@ class IndexController { $service = new \app\common\service\UsedCarCrawlService(); - $result = $service->iUrl('https://www.dongchedi.com/usedcar/21557932'); + $result = $service->iUrl('https://www.dongchedi.com/usedcar/21725410'); // $result = $service->doData(); // $result = $service->decodeDongchediPrice('.'); - // $result = $service->classifyEnergy('插电式混合动力'); + // $result = $service->detectEnergyType('油电混合'); return json($result); }