关键字抓取

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