关键字抓取

This commit is contained in:
2026-03-05 16:21:22 +08:00
parent 0d895688c3
commit 1720aec385
+34 -12
View File
@@ -617,22 +617,44 @@ class UsedCarCrawlService
$html = $this->fetchHtml($url);
$ql = QueryList::html($html);
// 解析车型列表
$ql->find('div.carModelsPager__EiwGC a.model-item__1r8j')->each(function ($item) use (&$models) {
$modelName = trim($item->text());
$modelUrl = $item->attr('href');
// 使用 Tailwind CSS 类名选择器(懂车帝使用 Tailwind)
$nodes = $ql->find('div.tw-grid div.tw-relative div.tw-items-center>a');
$texts = $nodes->texts()->all();
$links = $nodes->attrs('href')->all();
if ($modelName) {
$models[] = [
'name' => $modelName,
'url' => $modelUrl ? 'https://www.dongchedi.com' . $modelUrl : '',
];
// 提取价格信息(可选)
$priceNodes = $ql->find('div.tw-grid div.tw-text-color-gray-800');
$prices = $priceNodes->texts()->all();
foreach ($texts as $index => $text) {
$modelName = trim($text);
if (empty($modelName)) {
continue;
}
});
$link = $links[$index] ?? '';
if (!empty($link) && strpos($link, 'http') !== 0) {
$link = 'https://www.dongchedi.com' . $link;
}
$priceStr = $prices[$index] ?? '';
$msrp = 0;
if (!empty($priceStr)) {
$msrp = (int)(str_replace("万", "", $priceStr) * 10000);
}
$models[] = [
'id' => $index,
'name' => $modelName,
'url' => $link,
'msrp' => $msrp,
];
}
return $models;
} catch (\Exception) {
// 车型解析失败,返回空数组
} catch (\Exception $e) {
// 车型解析失败,记录日志并返回空数组
error_log('获取车型列表失败: ' . $e->getMessage());
return [];
}
}