移动端url抓取懂车帝车辆
This commit is contained in:
@@ -128,6 +128,25 @@ class UsedCarController extends BaseController
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动端链接抓取
|
||||
* @param Request $request
|
||||
*/
|
||||
public function crawlMobileCar(Request $request)
|
||||
{
|
||||
try {
|
||||
$urlShare = $request->get('url');
|
||||
|
||||
$finalUrl = (new UsedCarCrawlSingle())->getFinaUrl($urlShare);
|
||||
|
||||
$request->setGet('url', $finalUrl);
|
||||
$data = $this->crawlCar($request);
|
||||
return $data;
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function crawlCar(Request $request)
|
||||
{
|
||||
try {
|
||||
|
||||
@@ -36,10 +36,50 @@ class UsedCarCrawlSingle
|
||||
'headers' => self::DEFAULT_HEADERS,
|
||||
'timeout' => 10,
|
||||
]);
|
||||
|
||||
return (string)$response->getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取移动端最终url
|
||||
*
|
||||
* @param string $url
|
||||
* @return string
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function getFinaUrl(string $url): string
|
||||
{
|
||||
$client = new Client([
|
||||
'verify' => false,
|
||||
]);
|
||||
$response = $client->get($url, [
|
||||
'allow_redirects' => ['track_redirects' => true],
|
||||
]);
|
||||
|
||||
$history = $response->getHeader('X-Guzzle-Redirect-History');
|
||||
$finalUrl = end($history);
|
||||
|
||||
$sku_id = $this->getSkuIdFromUrl($finalUrl);
|
||||
|
||||
return 'https://www.dongchedi.com/usedcar/'.$sku_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从url获取sku_id
|
||||
* @param string $url
|
||||
* @return string|null
|
||||
*/
|
||||
public function getSkuIdFromUrl(string $url): ?string
|
||||
{
|
||||
$query = parse_url($url, PHP_URL_QUERY);
|
||||
if (!$query) {
|
||||
return null;
|
||||
}
|
||||
|
||||
parse_str($query, $params);
|
||||
|
||||
return $params['sku_id'] ?? null;
|
||||
}
|
||||
|
||||
public function crawlCar(string $url){
|
||||
$html = $this->fetchHtml($url);
|
||||
if ($html === '' || str_contains($html, '404 Not Found')) {
|
||||
|
||||
+1
-2
@@ -68,12 +68,11 @@ Route::group('/common', function () {
|
||||
Route::any('/destroy', [app\common\controller\UsedCarController::class, 'destroy']);
|
||||
Route::any('/crawl', [app\common\controller\UsedCarController::class, 'crawlKeyword']);
|
||||
Route::any('/url_crawl', [app\common\controller\UsedCarController::class, 'crawlCar']);
|
||||
Route::any('/mobile/url_crawl', [app\common\controller\UsedCarController::class, 'crawlMobileCar']);
|
||||
});
|
||||
|
||||
Route::group('/user', function () {
|
||||
Route::any('/info', [app\common\controller\UserController::class, 'show']);
|
||||
Route::any('/edit', [app\common\controller\UserController::class, 'edit']);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user