diff --git a/app/common/controller/UsedCarController.php b/app/common/controller/UsedCarController.php index 2b1365b..d5c4ee6 100644 --- a/app/common/controller/UsedCarController.php +++ b/app/common/controller/UsedCarController.php @@ -154,7 +154,8 @@ class UsedCarController extends BaseController if (empty($url)){ throw new BusinessException('url不能为空'); } - $info = (new UsedCarCrawlSingle())->crawlCar($url); + //$info = (new UsedCarCrawlSingle())->crawlCar($url); + $info = $this->service->selectCraw($url); $data = [ 'user_id' => $request->user['id'], 'product_name' => $info['title'], diff --git a/app/common/service/UsedCarService.php b/app/common/service/UsedCarService.php index a3692ec..7e29b9b 100644 --- a/app/common/service/UsedCarService.php +++ b/app/common/service/UsedCarService.php @@ -2,6 +2,7 @@ namespace app\common\service; +use app\common\utils\crawl\service\UsedCarCrawlSingle; use app\common\validate\UsedCarValidate; use app\common\traits\crud\CrudServiceTrait; use app\common\model\UsedCar; @@ -421,4 +422,38 @@ class UsedCarService return $path; } + + /** + * 选择车辆抓取方式 + * @param string $url + */ + public function selectCraw(string $url){ + $usedCarCrawl = new UsedCarCrawlSingle(); + switch (true){ + case str_contains($url,'dongchedi.com'): //懂车帝pc端 + $info = $usedCarCrawl->crawlCar($url); + break; + case str_contains($url,'dcdapp.com'): //懂车帝移动端 + $finalUrl = $usedCarCrawl->getPcUrl($url); //获取pc端页面链接 + if(empty($finalUrl)){ + throw new \Exception('链接解析错误'); + } + $info = $usedCarCrawl->crawlCar($finalUrl); + break; + case str_contains($url,'t.zijieimg.com'): //懂车帝移动端分享链接 + $finalUrl = $usedCarCrawl->getFinaUrl($url); //解析抖音短链,并获取pc端页面链接 + if(empty($finalUrl)){ + throw new \Exception('链接解析错误'); + } + $info = $usedCarCrawl->crawlCar($finalUrl); + break; + case str_contains($url,'che168.com'): //汽车之家 + throw new \Exception('加紧开发中'); + break; + default: + Log::info('未知链接:'.$url); + throw new \Exception('加紧开发中'); + } + return $info; + } } diff --git a/app/common/utils/crawl/service/UsedCarCrawlSingle.php b/app/common/utils/crawl/service/UsedCarCrawlSingle.php index 0d6bf26..1f45f1b 100644 --- a/app/common/utils/crawl/service/UsedCarCrawlSingle.php +++ b/app/common/utils/crawl/service/UsedCarCrawlSingle.php @@ -40,7 +40,7 @@ class UsedCarCrawlSingle } /** - * 获取移动端最终url + * 获取懂车帝移动端分享短链最终url * * @param string $url * @return string @@ -58,17 +58,17 @@ class UsedCarCrawlSingle $history = $response->getHeader('X-Guzzle-Redirect-History'); $finalUrl = end($history); - $sku_id = $this->getSkuIdFromUrl($finalUrl); + $pcUrl = $this->getPcUrl($finalUrl); - return 'https://www.dongchedi.com/usedcar/'.$sku_id; + return $pcUrl; } /** - * 从url获取sku_id + * 从移动端url获取sku_id * @param string $url * @return string|null */ - public function getSkuIdFromUrl(string $url): ?string + public function getPcUrl(string $url): ?string { $query = parse_url($url, PHP_URL_QUERY); if (!$query) { @@ -77,9 +77,15 @@ class UsedCarCrawlSingle parse_str($query, $params); - return $params['sku_id'] ?? null; + return !empty($params['sku_id']) ? 'https://www.dongchedi.com/usedcar/'.$params['sku_id'] : null; } + /** + * 抓取懂车帝二手车信息 + * @param string $url + * @return array + * @throws GuzzleException + */ public function crawlCar(string $url){ $html = $this->fetchHtml($url); if ($html === '' || str_contains($html, '404 Not Found')) {