Пример парсера с drom в json

class Parser

{

```
   public static $models = [];

```

```
   public static $domen = 'https://www.drom.ru';

```

```
   public static function parseTth($model_external_url, $file_name)

```

```
   {

```

```
       $page_obj = phpQuery::newDocumentHTML(self::downloadPage($model_external_url));

```

```
       $modification_arr = [];

```

```
       foreach ($page_obj->find('.b-table tr') as $tr) {

```

 echo "

```
";

            $link_td = pq($tr)->find('td:eq(1)');

            $price_td = pq($tr)->find('td:eq(3)')->text();

            $price = (int)filter_var($price_td, FILTER_SANITIZE_NUMBER_INT);

            $link = pq(pq($link_td)->find('a'))->attr('href');

            $modification_name = pq(pq($link_td)->find('a'))->text();

            $year = pq($tr)->find('td:eq(2)')->text();

            $year = str_replace(" ", "", htmlentities($year, null, 'utf-8'));

            $year = html_entity_decode($year);

            $year = trim(str_replace(["\n", "\r"], ' ', $year));

            if (preg_match('/-$/', $year))

            {

                sleep(2);

                echo "\n\n\n\n-------------------------------------------------------------------------\n";

                echo $link . "\n";

                echo $price . "\n";

                echo $modification_name . "\n";

                $modification_arr[] = self::parseTthModification($link, $price, $modification_name);

            }

        }

        $json = json_encode($modification_arr);

        file_put_contents($file_name.'.json', $json);

    }

    public static function  parseTthModification($link, $price, $modification_name)

    {

        $modification = [

            'name' => $modification_name,

            'modification' => $price

        ];

        $page_obj = phpQuery::newDocumentHTML(self::downloadPage(self::$domen . $link));

        $group_tth_sort = 10;

        $tth_sort = 10;

        $groups_tth = [];

        foreach ($page_obj->find('tr') as $tr) {

            if (pq($tr)->hasClass('b-text_size_xl')) continue;// подзаголовок пропускаем

            if (pq($tr)->find('td:eq(0)')->attr('colspan') == 2) {

                $category_name = pq($tr)->find('td:eq(0)')->text();

                echo $category_name . "\n";

                $group_tth = [

                    "name" => $category_name,

                    'sortorder' => $group_tth_sort,

                    'tth'   => []

                ];

                $group_tth_sort = $group_tth_sort + 10;

            } else {

                $spec_name = pq($tr)->find('td:eq(0)')->text();

                pq($tr)->find('td:eq(1) a')->remove();

                $spec_value = trim(pq($tr)->find('td:eq(1)')->text());

                if (pq($tr)->find('td:eq(1)')->find('use')->length > 0) {

                    $icon_name = pq($tr)->find('td:eq(1)')->find('use')->attr('xlink:href');

                    if ($icon_name === '#yes') {

                        $spec_value = '+ '.$spec_value;

                    }

                    if ($icon_name === '#option') {

                        $spec_value = 'Опция производителя';

                    }

                }

                if (strpos($spec_name, 'Рекомендованная цена новой') !== false ){

                    continue;

                }

                if ($spec_value == '—') {

                    continue;

                }

               $group_tth['tth'][] = [

                    'spec_name' => $spec_name,

                    'sortorder' => $tth_sort,

                    'value' => $spec_value

                ];

                $tth_sort = $tth_sort + 10;

                if (!empty($group_tth)){

                    $modification['tth'][] = $group_tth;

                }

                echo '---- ' . $spec_name . "                    " . $spec_value . "\n";

            }

        }

        echo $modification_name;

        return $modification;

    }

    public static function parseModifiacation($model_external_url, $file_name){

        $page_obj = phpQuery::newDocumentHTML(self::downloadPage($model_external_url));

        $modification_arr = [];

        $header_counter = 0;

        foreach ($page_obj->find('.b-table tr') as $tr) {

            if (pq($tr)->find('th[colspan]')->length > 0){

                $header_counter++;

                $header_model_name = trim(pq($tr)->text());

                echo $header_model_name."\n";

                $group_modification = [

                    'name'  => $header_model_name

                ];

                $modification_arr[$header_counter]['group_name'] = $group_modification;

            }

            if (pq($tr)->find('td')->length > 0){

                $model_name = trim(pq($tr)->find('td:eq(1)')->text());

                $year = pq($tr)->find('td:eq(2)')->text();

                $year = str_replace(" ", "", htmlentities($year, null, 'utf-8'));

                $year = html_entity_decode($year);

                $year = trim(str_replace(["\n", "\r"], ' ', $year));

                $price = preg_replace('/[^0-9]/', *, (pq($tr)->find('td:eq(3)')->text()));;*

                if (preg_match('/-$/', $year)){

                    echo "-----".$model_name."*".$year."*\n";

                    $complectation = [

                        'name'  => $model_name,

                        'year'  => $year,

                        'price' => $price

                    ];

                    $modification_arr[$header_counter]['complectation'][] = $complectation;

                }

            }

        }

        $result = [];

        foreach ($modification_arr as $modification){

            if (!empty($modification['complectation'])){

                $result[] = $modification;

            }

        }

        $json = json_encode($result);

        file_put_contents($file_name.'.json', $json);

    }

    public  static function parseComplectationWithoutAdmin($url_from, $compectation_parent_model_id){

        new simpleParser();

        Parser::parseModifiacation($url_from, $compectation_parent_model_id);

        $modifications = json_decode(file_get_contents($compectation_parent_model_id . '.json'), 1);

        foreach ($modifications as $modification_group){

            $group_id = simpleModels::addModel([

                'name' => $modification_group['group_name']['name'],

                'code' => ParserExt::toCode($modification_group['group_name']['name']),

                'parent_id' => $compectation_parent_model_id,

            ]);

            foreach($modification_group['complectation'] as $modification){

                $modification_id = simpleModels::addModel([

                    'name' => $modification['name'],

                    'code' => ParserExt::toCode($modification['name']),

                    'parent_id' => $group_id,

                ]);

                simpleModels::setModelInfo(

                    [

                        'model_id' => $modification_id,

                        'price' => $modification['price'],

                        'description' => $modification['year'],

                    ]

                );

            }

        }

    }

    public  static function parseTthWithoutAdmin($url_from, $tth_model_id){

        new simpleParser();

        Parser::parseTth($url_from, $tth_model_id);

        echo "\nparsing to model\n";

        $modifications = json_decode(file_get_contents($tth_model_id.'.json'), true);

        simpleModels::clearChildModels($tth_model_id);

        $tth_parent_id = $tth_model_id;

        $modification_counter = 1;

        $counter = 0;

        foreach ($modifications as $modification){

            $counter++;

            if (true) {

                echo $modification['name']."\n";

                $model_id = simpleModels::addModel([

                    'name' => $modification['name'],

                    'code' => ParserExt::toCode($modification['name']),

                    'parent_id' => $tth_parent_id,

                ]);

                simpleModels::setModelInfo(

                    [

                        'model_id' => $model_id,

                        'price' => $modification['modification']

                    ]

                );

                $group_tth_sort = 10;

                $tth_sort = 10;

                foreach ($modification['tth'] as $item_group) {

                    echo "group: ".$item_group['name']."\n";

                    $group_tth_id = simpleModels::addTTX([

                        'name' => $item_group['name'],

                        'parent_id' => 1,

                        'sortorder' => $group_tth_sort,

                    ]);

                    $group_tth_sort = $group_tth_sort + 10;

                    foreach ($item_group['tth'] as $tth) {

                        $tth_id = simpleModels::addTTX([

                            "name" => $tth['spec_name'],

                            "parent_id" => $group_tth_id,

                            'sortorder' => $tth_sort,

                        ]);

                        simpleModels::addTTXValue(

                            [

                                'model_id' => $model_id,

                                'ttx_id' => $tth_id,

                                'value' => $tth['value'],

                            ]

                        );

                        $tth_sort = $tth_sort + 10;

                    }

                }

            }

            $modification_counter ++;

        }

    }

    public static function downloadPage($url){

        $file_name = md5($url).'.html';

        if (file_exists($file_name)){

            return file_get_contents($file_name);

        }

        $data = file_get_contents($url);

        file_put_contents($file_name, $data);

        return $data;

    }

}

//Parser::parseTth('https://www.drom.ru/catalog/volvo/xc40/g\_2017\_7858/', 'ХС40_tth');

//Parser::parseModifiacation('https://www.drom.ru/catalog/volvo/xc40/g\_2017\_7858/', 'xc40_compl');

//  Parser::parseTthWithoutAdmin('https://www.drom.ru/catalog/volvo/s60/g\_2018\_8416/', 762);

 Parser::parseTthWithoutAdmin('https://www.drom.ru/catalog/volvo/s90/g\_2020\_12118/', 451);

 Parser::parseTthWithoutAdmin('https://www.drom.ru/catalog/volvo/v60/g\_2018\_8209/', 781);

 Parser::parseTthWithoutAdmin('https://www.drom.ru/catalog/volvo/v90/g\_2020\_12119/', 783);

 Parser::parseTthWithoutAdmin('https://www.drom.ru/catalog/volvo/xc40/g\_2017\_7858/', 584);

 Parser::parseTthWithoutAdmin('https://www.drom.ru/catalog/volvo/xc60/g\_2017\_7513/', 678);

 Parser::parseTthWithoutAdmin('https://www.drom.ru/catalog/volvo/xc90/g\_2019\_9574/', 203);

 //Parser::parseComplectationWithoutAdmin('https://www.drom.ru/catalog/volvo/s60/g\_2018\_8416/', 821);

 //Parser::parseComplectationWithoutAdmin('https://www.drom.ru/catalog/volvo/s90/g\_2020\_12118/', 822);

 //Parser::parseComplectationWithoutAdmin('https://www.drom.ru/catalog/volvo/v60/g\_2018\_8209/', 823);

 // Parser::parseComplectationWithoutAdmin('https://www.drom.ru/catalog/volvo/v90/g\_2020\_12119/', 824);

 // Parser::parseComplectationWithoutAdmin('https://www.drom.ru/catalog/volvo/xc40/g\_2017\_7858/', 621);

 // Parser::parseComplectationWithoutAdmin('https://www.drom.ru/catalog/volvo/xc60/g\_2017\_7513/', 825);

 // Parser::parseComplectationWithoutAdmin('https://www.drom.ru/catalog/volvo/xc90/g\_2019\_9574/', 826);
```