Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- ini_set("memory_limit", "4000M");
- set_time_limit(0);
- include_once('lib/phpQuery.php');
- $url = $_POST['link_rubrika'];
- $time_start = microtime(true);
- // for count items
- $link_content = file_get_contents($url);
- $total = phpQuery::newDocument($link_content)->find('#demo-container')->attr('data-nb-adverts-total');
- $k = ceil($total/20);
- function getViewsItem($id) {
- return json_decode(file_get_contents('https://kolesa.kz/ms/views/kolesa/live/'.$id.'/'))->data->{$id}->nb_views;
- }
- function getPhones($id) {
- $ch = curl_init('https://kolesa.kz/a/ajaxPhones/?id='.$id);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('referer: https://kolesa.kz/', 'x-requested-with: XMLHttpRequest'));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_HEADER, false);
- $html = curl_exec($ch);
- curl_close($ch);
- //$arr_phones = explode(', ', json_decode($html)->data->model->phone);
- return json_decode($html)->data->model->phone;
- }
- while($k>0) {
- $html = file_get_contents($url."&page=".$k);
- $document = phpQuery::newDocument($html);
- foreach ($document->find('.a-elem') as $i => $item) {
- $id = pq($item)->attr('data-id');
- $getDetailPage = phpQuery::newDocument(file_get_contents("https://kolesa.kz/a/show/".$id));
- // создаём массив со всеми параметрами товара
- if (!preg_match('~\swindow.digitalData = (\{.+\});\n~', $getDetailPage, $match) || !isset($match[1])) {
- throw new Exception('something went wrong');
- }
- $pageData = json_decode($match[1], true);
- $brand = trim($pageData['product']['attributes']['brand']);
- $model = trim($pageData['product']['attributes']['model']);
- $year = trim($getDetailPage->find('.offer__title span.year')->html());
- $price = trim($pageData['product']['unitPrice']);
- $link = trim($pageData['product']['url']);
- $date = new DateTime(trim($pageData['product']['publicationDate']));
- $pubDate = $date->format('Y/m/d H:i:s');
- foreach ($getDetailPage->find('.offer__parameters dl') as $prop) {
- $props[trim(pq($prop)->find('dt')->attr('title'))] = trim(pq($prop)->find('dd')->html());
- }
- $city = $props['Город'];
- $kuzov = $props['Кузов'];
- $obiem_dizka = $props['Объем двигателя, л'];
- $views = getViewsItem($id);
- $phones = getPhones($id);
- $data[] = [ $id, $link, $brand, $model, $price, $year, $city, $kuzov, $obiem_dizka, $pubDate, $views, $phones ];
- unset($getDetailPage, $props);
- }
- unset($html);
- $k--;
- }
- $arrForTable = [ 'data' => $data ];
- $time_end = microtime(true);
- $time = $time_end - $time_start;
- // Переводим массив в JSON
- echo json_encode($arrForTable);
- //echo "Сбор ".count($data)." объявлений завершен.\nПрошло ".$time." секунд\n";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement