修改爬虫服务
This commit is contained in:
@@ -46,7 +46,7 @@ class UsedCarCrawlService
|
|||||||
if (!$raw) {
|
if (!$raw) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
echo '开始抓取~:'.date('Y-m-d H:i:s').PHP_EOL;
|
|
||||||
$html = $this->getHtml($raw['url']);
|
$html = $this->getHtml($raw['url']);
|
||||||
|
|
||||||
switch ($raw['scene']) {
|
switch ($raw['scene']) {
|
||||||
@@ -54,15 +54,11 @@ echo '开始抓取~:'.date('Y-m-d H:i:s').PHP_EOL;
|
|||||||
$info = $this->getUsedDetail($html, $raw['url']);
|
$info = $this->getUsedDetail($html, $raw['url']);
|
||||||
$id = $this->saveUsedcarBasicToDb($info);
|
$id = $this->saveUsedcarBasicToDb($info);
|
||||||
$this->iUrl($info['car_id_url'], 'config', $id);
|
$this->iUrl($info['car_id_url'], 'config', $id);
|
||||||
|
|
||||||
echo '一阶段 抓取成功~:'.date('Y-m-d H:i:s').PHP_EOL;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'config':
|
case 'config':
|
||||||
$data = $this->getConfigDetail($html);
|
$data = $this->getConfigDetail($html);
|
||||||
$this->saveUsedcarConfigToDb($data, $raw['id']);
|
$this->saveUsedcarConfigToDb($data, $raw['id']);
|
||||||
|
|
||||||
echo '二阶段 抓取成功~:'.date('Y-m-d H:i:s').PHP_EOL;
|
|
||||||
break;
|
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'] ?? '';
|
$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'] ?? '';
|
$importantText = $jsonArray['props']['pageProps']['skuDetail']['important_text'] ?? '';
|
||||||
$importantTextArray = explode(' | ', $importantText);
|
$importantTextArray = explode(' | ', $importantText);
|
||||||
@@ -186,8 +184,8 @@ echo '开始抓取~:'.date('Y-m-d H:i:s').PHP_EOL;
|
|||||||
$info['car_id_url'] = $carIdUrl;
|
$info['car_id_url'] = $carIdUrl;
|
||||||
$info['used_detail'] = $jsonText;
|
$info['used_detail'] = $jsonText;
|
||||||
$info['format_shop_info'] = json_encode($formatShopInfo);
|
$info['format_shop_info'] = json_encode($formatShopInfo);
|
||||||
$info['fuel_form'] = $fuelForm;
|
$info['fuel_form'] = $resultFuelForm;
|
||||||
$info['classify_energy'] = $this->classifyEnergy($fuelForm);
|
$info['classify_energy'] = $this->detectEnergyType($resultFuelForm);
|
||||||
$info['usage_mileage'] = $usageMileage;
|
$info['usage_mileage'] = $usageMileage;
|
||||||
$info['brand_name'] = $brandName;
|
$info['brand_name'] = $brandName;
|
||||||
$info['series_name'] = $seriesName;
|
$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 = ['汽油', '柴油', '天然气'];
|
if (function_exists('mb_internal_encoding')) {
|
||||||
$re = ['纯电', '电动', '氢能', '氢燃料', '太阳能', '油电混动', '混动', '插电混动', '插电式', '增程式', '增程'];
|
mb_internal_encoding('UTF-8');
|
||||||
|
|
||||||
// 标准化输入:去除空格等
|
|
||||||
$input = trim($input);
|
|
||||||
|
|
||||||
// 1. 精确或子串匹配(优先)
|
|
||||||
foreach ($re as $keyword) {
|
|
||||||
if (strpos($input, $keyword) !== false) {
|
|
||||||
return 'RE'; // 新能源
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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%)
|
// 2. 再判断“普通油电混动”——按照你的业务,一律归为传统能源
|
||||||
$bestMatch = null;
|
foreach ($hybridKeywords as $kw) {
|
||||||
$bestScore = 0;
|
if ($contains($text, $kw)) {
|
||||||
$bestType = null;
|
return 'CE';
|
||||||
|
|
||||||
// 检查 RE
|
|
||||||
foreach ($re as $keyword) {
|
|
||||||
similar_text($input, $keyword, $percent);
|
|
||||||
if ($percent > $bestScore && $percent >= 80) {
|
|
||||||
$bestScore = $percent;
|
|
||||||
$bestMatch = $keyword;
|
|
||||||
$bestType = 'RE';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查 CE
|
// 3. 再判断传统能源(纯燃油/气)
|
||||||
foreach ($ce as $keyword) {
|
foreach ($traditionalKeywords as $kw) {
|
||||||
similar_text($input, $keyword, $percent);
|
if ($contains($text, $kw)) {
|
||||||
if ($percent > $bestScore && $percent >= 80) {
|
return 'CE';
|
||||||
$bestScore = $percent;
|
|
||||||
$bestMatch = $keyword;
|
|
||||||
$bestType = 'CE';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $bestType ?? 'UNKNOWN'; // 未识别
|
// 4. 都没匹配上
|
||||||
|
return 'UNKNOWN';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -161,13 +161,13 @@ class IndexController
|
|||||||
{
|
{
|
||||||
$service = new \app\common\service\UsedCarCrawlService();
|
$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->doData();
|
||||||
|
|
||||||
// $result = $service->decodeDongchediPrice('.');
|
// $result = $service->decodeDongchediPrice('.');
|
||||||
|
|
||||||
// $result = $service->classifyEnergy('插电式混合动力');
|
// $result = $service->detectEnergyType('油电混合');
|
||||||
|
|
||||||
return json($result);
|
return json($result);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user