Advertisement
SpeakeazyYT

Untitled

Sep 9th, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. <?
  2. ini_set("memory_limit", "4000M");
  3. set_time_limit(0);
  4. include_once('lib/phpQuery.php');
  5.  
  6. $url = $_POST['link_rubrika'];
  7.  
  8. $time_start = microtime(true);
  9.  
  10. // for count items
  11. $link_content = file_get_contents($url);
  12. $total = phpQuery::newDocument($link_content)->find('#demo-container')->attr('data-nb-adverts-total');
  13. $k = ceil($total/20);
  14.  
  15. function getViewsItem($id) {
  16.     return json_decode(file_get_contents('https://kolesa.kz/ms/views/kolesa/live/'.$id.'/'))->data->{$id}->nb_views;
  17. }
  18.  
  19. function getPhones($id) {
  20.     $ch = curl_init('https://kolesa.kz/a/ajaxPhones/?id='.$id);
  21.     curl_setopt($ch, CURLOPT_HTTPHEADER, array('referer: https://kolesa.kz/', 'x-requested-with: XMLHttpRequest'));
  22.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  23.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  24.     curl_setopt($ch, CURLOPT_HEADER, false);
  25.     $html = curl_exec($ch);
  26.     curl_close($ch);
  27.      
  28.     //$arr_phones = explode(', ', json_decode($html)->data->model->phone);
  29.     return json_decode($html)->data->model->phone;
  30. }
  31.  
  32. while($k>0) {
  33.     $html = file_get_contents($url."&page=".$k);
  34.     $document = phpQuery::newDocument($html);
  35.    
  36.     foreach ($document->find('.a-elem') as $i => $item) {
  37.         $id = pq($item)->attr('data-id');
  38.         $getDetailPage = phpQuery::newDocument(file_get_contents("https://kolesa.kz/a/show/".$id));
  39.        
  40.         // создаём массив со всеми параметрами товара
  41.         if (!preg_match('~\swindow.digitalData = (\{.+\});\n~', $getDetailPage, $match) || !isset($match[1])) {
  42.             throw new Exception('something went wrong');
  43.         }
  44.  
  45.         $pageData = json_decode($match[1], true);
  46.        
  47.         $brand = trim($pageData['product']['attributes']['brand']);
  48.         $model = trim($pageData['product']['attributes']['model']);
  49.         $year = trim($getDetailPage->find('.offer__title span.year')->html());
  50.         $price = trim($pageData['product']['unitPrice']);
  51.        
  52.         $link = trim($pageData['product']['url']);
  53.  
  54.         $date = new DateTime(trim($pageData['product']['publicationDate']));
  55.         $pubDate = $date->format('Y/m/d H:i:s');
  56.        
  57.         foreach ($getDetailPage->find('.offer__parameters dl') as $prop) {
  58.             $props[trim(pq($prop)->find('dt')->attr('title'))] = trim(pq($prop)->find('dd')->html());
  59.         }
  60.        
  61.         $city = $props['Город'];
  62.         $kuzov = $props['Кузов'];
  63.         $obiem_dizka = $props['Объем двигателя, л'];
  64.        
  65.         $views = getViewsItem($id);
  66.         $phones = getPhones($id);
  67.        
  68.         $data[] = [ $id, $link, $brand, $model, $price, $year, $city, $kuzov, $obiem_dizka, $pubDate, $views, $phones ];
  69.        
  70.         unset($getDetailPage, $props);
  71.     }
  72.    
  73.     unset($html);
  74.     $k--;
  75. }
  76.  
  77. $arrForTable = [ 'data' => $data ];
  78.  
  79. $time_end = microtime(true);
  80. $time = $time_end - $time_start;
  81.  
  82. // Переводим массив в JSON
  83. echo json_encode($arrForTable);
  84.  
  85. //echo "Сбор ".count($data)." объявлений завершен.\nПрошло ".$time." секунд\n";
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement