Files
fdesk/app/common/service/CrawlService.php
T

76 lines
2.1 KiB
PHP

<?php
namespace app\common\service;
use app\common\logic\BrandLogic;
use QL\QueryList;
class CrawlService
{
public function brandSeries($brand)
{
$url = "https://www.dongchedi.com/search?currTab=6&keyword=".$brand;
$ql = QueryList::get($url );
$series = [];
$ql->find('div.card-name')->texts()->map(function($item) use(&$series) {
$series[] = $item;
});
return $series;
}
public function seriesModel($series)
{
$href = $this->modelSearch($series);
//例如 $seriesUrl = "https://www.dongchedi.com/auto/series/4865?anchorId=carModels"
if (empty($href)) {
return [];
}
$seriesUrl = (strpos($href, 'http') === 0) ? $href : ("https://www.dongchedi.com" . $href);
$ql = QueryList::get($seriesUrl);
$nodes = $ql->find('div.tw-grid div.tw-relative div.tw-items-center>a');
$texts = $nodes->texts()->all();
$links = $nodes->attrs('href')->all();
$nodes = $ql->find('div.tw-grid div.tw-text-color-gray-800');
$price = $nodes->texts()->all();
$results = [];
foreach ($texts as $index => $text) {
$link = $links[$index] ?? '';
if (!empty($link) && strpos($link, 'http') !== 0) {
$link = 'https://www.dongchedi.com' . $link;
}
$results[] = [
'title' => trim($text),
'link' => $link,
'msrp' => (int)(str_replace("万", "", $price[$index]) * 10000) ?? 0,
];
}
return $results;
}
// public function modelDesc(array $models)
// {
//
// }
/**
* 返回车系地址
* @param $series
* @return mixed|string
*/
public function modelSearch($series)
{
$url = "https://www.dongchedi.com/search?keyword=".$series;
$ql = QueryList::get($url );
$href = '';
$ql->find('a.fold-btn')->attrs('href')
->map(function ($item) use(&$href) {
$href = $item;
});
return $href;
}
}