Merge branch 'feature/sku_crawl' into develop
This commit is contained in:
@@ -13,6 +13,7 @@ use app\common\service\VehVehicleSkuService;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\BrandSeries;
|
||||
use app\common\utils\CrudUtil;
|
||||
use GuzzleHttp\Client;
|
||||
use support\Log;
|
||||
use support\Request;
|
||||
use Webman\RedisQueue\Redis;
|
||||
@@ -134,12 +135,117 @@ class BrandSeriesService
|
||||
}
|
||||
|
||||
/**
|
||||
* 抓取车型数据
|
||||
* 抓取车型数据(通过接口)
|
||||
* @param Request $request
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function crawlModel(Request $request)
|
||||
public function crawlModel(Request $request): array
|
||||
{
|
||||
$series_id = $request->get('brand_series_id');
|
||||
|
||||
// 验证品牌是否存在
|
||||
$series = BrandSeries::where('id', $series_id)->first();
|
||||
if (!$series) {
|
||||
throw new \Exception("系列ID {$series_id} 不存在");
|
||||
}
|
||||
|
||||
// 获取品牌信息
|
||||
$series = BrandSeries::where('id', $series_id)->first();
|
||||
if (!$series) {
|
||||
throw new \Exception("系列ID {$series_id} 不存在");
|
||||
}
|
||||
|
||||
// 获取品牌信息
|
||||
$brand = Brand::where('id', $series->brand_id)->first();
|
||||
|
||||
// 执行抓取
|
||||
$crawlService = new CrawlService();
|
||||
$href = $crawlService->modelSearch($brand->brand_name . ' ' . $series->series_name);
|
||||
$seriesId = $this->extractSeriesId($href);
|
||||
$client = new Client([
|
||||
'verify' => false, // 禁用 SSL 验证
|
||||
'timeout' => 10
|
||||
]);
|
||||
try {
|
||||
$response = $client->get('https://www.dongchedi.com/motor/pc/car/series/car_list', [
|
||||
'query' => ['series_id' => $seriesId],
|
||||
'headers' => [
|
||||
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
||||
]
|
||||
]);
|
||||
$data = json_decode($response->getBody()->getContents(), true);
|
||||
if($data['status'] != 0 || (!isset($data['data']) && !isset($data['data']['tab_list']))){
|
||||
Log::error('抓取车型数据失败' . $data['message'], $data);
|
||||
throw new \Exception('抓取车型数据失败' . $data['message']);
|
||||
}
|
||||
$models = [];
|
||||
foreach ($data['data']['tab_list'] as $item) {
|
||||
if($item['tab_text'] == '在售' || $item['tab_text'] == '停售'){
|
||||
unset($item['data'][0]);
|
||||
$models = array_merge($models, $item['data']);
|
||||
}
|
||||
}
|
||||
foreach ($models as $key => $model) {
|
||||
if(!isset($model["info"]['car_name'])){
|
||||
unset($models[$key]);
|
||||
continue;
|
||||
}
|
||||
$official_price = 0;
|
||||
if(!isset($model["info"]['official_price'])){
|
||||
$official_price = (int)(str_replace("万", "", $model["info"]['official_price_str']) * 10000) ?? 0;
|
||||
}else{
|
||||
$official_price = $model["info"]['official_price'] * 10000;
|
||||
}
|
||||
$models[$key] = [
|
||||
'model_name' => $model["info"]['year'] . '款 ' .$model["info"]['car_name'],
|
||||
'brand_name' => $model["info"]['brand_name'],
|
||||
'year' => $model["info"]['year'],
|
||||
'msrp' => $official_price,
|
||||
];
|
||||
}
|
||||
return $models;
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception($e->getMessage());
|
||||
}
|
||||
|
||||
|
||||
// return $models;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从懂车帝URL中提取series ID
|
||||
* @param string $url 完整的URL
|
||||
* @return string|null series ID或null
|
||||
*/
|
||||
function extractSeriesId($url) {
|
||||
// 解析URL获取路径部分
|
||||
$path = parse_url($url, PHP_URL_PATH);
|
||||
|
||||
if (!$path) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 按斜杠分割路径
|
||||
$pathParts = explode('/', trim($path, '/'));
|
||||
|
||||
// 查找 'series' 的位置,下一个元素就是series ID
|
||||
$seriesIndex = array_search('series', $pathParts);
|
||||
|
||||
if ($seriesIndex !== false && isset($pathParts[$seriesIndex + 1])) {
|
||||
return $pathParts[$seriesIndex + 1];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 抓取车型数据(通过页面抓取)
|
||||
* @param Request $request
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
/*public function crawlModel(Request $request)
|
||||
{
|
||||
$series_id = $request->get('brand_series_id');
|
||||
|
||||
@@ -167,14 +273,7 @@ class BrandSeriesService
|
||||
}
|
||||
|
||||
return $models;
|
||||
|
||||
// // 将抓取任务加入队列
|
||||
// Redis::send('series-crawl', [
|
||||
// 'brand_series_id' => $series
|
||||
// ]);
|
||||
//
|
||||
// return true;
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
@@ -203,7 +302,7 @@ class BrandSeriesService
|
||||
|
||||
foreach ($models as $item) {
|
||||
$modelData = [
|
||||
'brand_code' => $brand->brand_code ?? '',
|
||||
'brand_code' => $brand->brand_code,
|
||||
'brand_name' => $brand->brand_name,
|
||||
'series_code' => $series->series_code ?? '',
|
||||
'series_name' => $series->series_name ?? '',
|
||||
|
||||
Reference in New Issue
Block a user