Скрипт простой, пойдет как пример реализации парсера. Полученные марки авто складываются в базу.
class db {
private $host = 'localhost';
private $user = 'root';
private $pass = 'password';
private $base = 'parser';
private $url = "https://www.drive2.ru/cars/?all";
private $limit = 10; // Limit brands
/**
* Connect to database
* @return bool
*/
public function connect ()
{
$link = @mysqli_connect($this->host, $this->user, $this->pass, $this->base) or die("Error "); return $link;
}
/**
* Test only
*/
public function select ()
{
$query = "SELECT * FROM `brand` LIMIT 0, 100";
$result = mysqli_query($this->connect(), $query);
while($row = mysqli_fetch_array($result))
{
print_r($row);
}
}
/**
* @param $sql
* @return int|string
* Simple insert
*/
public function insert($sql)
{
$link = $this->connect();
@mysqli_query($link, $sql);
return mysqli_insert_id($link);
}
/**
* @param $url
* @return mixed
* Getting pages using CURL
*/
public function curlik($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
return $res; }
/**
* @param $brand
* @param bool $last_ins
* Get car models recursively
*/
public function get_child($brand, $last_ins = false) {
// Recursively check if has child with childs, get deepest
// if element has found, follow links if it has its
preg_match('/<a href=\"([\s\S]*?)"/', $brand, $link_res); // get brand page link
preg_match('/>(.*?)</', $brand, $brand_res); // get brand name && save to DB
// show childs if they exists
$content = $this->curlik('https://www.drive2.ru'.$link_res[1]);
preg_match('/<nav class="catalog-nav catalog-nav-short catalog-nav-closed">([\s\S]*?)<\/nav>/', $content, $res);
$children = explode(PHP_EOL, $res[1]); // string 2 array
// проверить каждый элемент, есть ли у него потомки
// если есть - рекурсивно вывести
// если нет, отобразить текущие
foreach ($children as $marka) {
if($marka) {
preg_match('/<a href=\"([\s\S]*?)"/', $marka, $marka_link);
preg_match('/>(.*?)</', $marka, $marka_name); // get brand name && save to DB
if(trim($marka_name[1]) !== '') {
$this->insert('INSERT INTO `model` (`brand_id`, `model_name`) VALUES ("'.$last_ins.'", "'.trim(htmlspecialchars($marka_name[1])).'")');
}
$this->get_child($marka, $last_ins); // recursive request
}
}
}
/**
* main action
*/
public function indexAction() {
$content = $this->curlik($this->url);
// brands list
// список брендов
preg_match('/<nav class="catalog-nav catalog-nav-short catalog-nav-closed">([\s\S]*?)<\/nav>/', $content, $res);
$brands = explode(PHP_EOL, $res[1]);
// перебираем брендов, берем из каждого список марок авто внутри тега <nav> при помощи ф-и get_childs
foreach ($brands as $k => $brand) {
if($brand) {
preg_match('/>(.*?)</', $brand, $bname);
if(trim($bname[1]) !== '') {
$last_ins = $this->insert('INSERT INTO `brand` (`name`) VALUES ("'.trim(htmlspecialchars($bname[1])).'")');
}
$this->get_child($brand, $last_ins);
}
// тут записать бренд в БД
if($k == $this->limit)
break;
}
}
}
Вызов парсера:
$parse = new db(); $parse->connect(); // $db->select(); $parse->indexAction();
Структура базы:


Прикрепляю как обычно архив со скриптом и базой.
| Файл | Описание | Размер файла: |
|---|---|---|
| 3 Кб |


