Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.60 KB | None | 0 0
  1. public function actionOlx()
  2.     {
  3.         $baseUrl = 'https://www.olx.ua/';
  4.         $pages = 10;
  5.  
  6.         for ($i = 1; $i < $pages; $i++) {
  7.             $url = $baseUrl . 'nedvizhimost/kvartiry-komnaty/arenda-kvartir-komnat/kvartira/kiev/q-%D0%B1%D0%B5%D0%B7-%D0%BA%D0%BE%D0%BC%D0%B8%D1%81%D1%81%D0%B8%D0%B8/?search%5Bdescription%5D=1&page=' . $i;
  8.  
  9.             $ch = curl_init($url);
  10.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  11.             curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0');
  12.             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  13.             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  14.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  15.             curl_setopt($ch, CURLOPT_VERBOSE, 1);
  16. //            curl_setopt($ch, CURLOPT_PROXY, '103.225.228.193:58732');
  17. //            curl_setopt($ch, CURLOPT_REFERER, 'https://google.com');
  18.             $html = curl_exec($ch);
  19.  
  20.             \phpQuery::newDocument($html);
  21.  
  22.             $catalogContent = pq('div.offer-wrapper')->find('a.detailsLink');
  23.  
  24.             foreach ($catalogContent as $k => $v) {
  25.                 $value = pq($v);
  26.                 $apartmentLink = $value->attr('href');
  27.                 $apartmentTitle = $value->find('img.fleft')->attr('alt');
  28.  
  29.                 $hash = Parser::makeHash($apartmentTitle);
  30.                 $isExist = Parser::findByHash($hash, $baseUrl);
  31.  
  32.                 if (!$isExist) {
  33.                     $cookie_path = $_SERVER['DOCUMENT_ROOT'] . '/cookie.dat';
  34.                     preg_match('|-ID(.*).html|', $apartmentLink, $id);
  35.  
  36.                     $apartment = curl_init($apartmentLink);
  37.                     $options = [
  38.                         CURLOPT_FOLLOWLOCATION => true, // +
  39.                         CURLOPT_RETURNTRANSFER => true,
  40.                         CURLOPT_HEADER => true, // false
  41.                         CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0",
  42. //                        CURLOPT_AUTOREFERER => true,
  43. //                        CURLOPT_CONNECTTIMEOUT => 120,
  44. //                        CURLOPT_TIMEOUT => 120,
  45.                         CURLOPT_SSL_VERIFYHOST => false, // +
  46.                         CURLOPT_SSL_VERIFYPEER => false, // +
  47.                         CURLOPT_VERBOSE => 1,
  48. //                        CURLOPT_PROXY => '103.225.228.193:58732',
  49. //                        CURLOPT_REFERER => 'https://google.com', // +
  50.                         CURLOPT_COOKIEFILE => $cookie_path,
  51.                         CURLOPT_COOKIEJAR => $cookie_path
  52.                     ];
  53.  
  54.                     curl_setopt_array($apartment, $options);
  55.  
  56.                     $apartmentContent = curl_exec($apartment);
  57.                     curl_close($apartment);
  58.  
  59.                     \phpQuery::newDocument($apartmentContent);
  60.  
  61.                     $apartmentImageLinks = pq('div.offerdescription')->find('div.photo-glow > img');
  62.                     $apartmentH1 = pq('div.offerdescription')->find('div.offer-titlebox > h1')->text();
  63.                     $apartmentText = pq('div.offerdescription')->find('div.descriptioncontent')->find('div.large')->text();
  64.                     $apartmentExternalInfo = pq('div.offerdescription')->find('div.descriptioncontent')->find('table.details')->text();
  65.                     $apartmentPrice = pq('div.price-label')->find('.xxxx-large')->text(); //xxxx-large not-arranged
  66.                     $apartmentAreaLocation = pq('a.show-map-link > strong')->text();
  67.                     $apartmentDetails = pq('div.offerdescription')->find('div.descriptioncontent')->text();
  68.  
  69.                     var_dump($apartmentDetails);
  70.                     exit;
  71.                     $typoCheck = 'Частного лица';
  72.  
  73.                     if (strpos($apartmentDetails, $typoCheck) !== false) {
  74.                         $imagesPathArray = [];
  75.                         foreach ($apartmentImageLinks as $key => $l) {
  76.                             $l = pq($l);
  77.  
  78.                             // пытаемся собрать изображения и создаем запись в таблице с квартирами
  79.                             $imageLink = $l->attr('src');
  80.                             $dir = \Yii::getAlias('@frontend/web/img/apartment/');
  81.                             $fullPath = $dir . $hash;
  82.  
  83.                             if (!file_exists($fullPath)) {
  84.                                 mkdir($fullPath, 0777, true);
  85.                             }
  86.  
  87.                             $hFileName = rand(1, 9999);
  88.  
  89.                             $context = stream_context_create([
  90.                                 "http" => [
  91.                                     "header" => "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Iron/31.0.1700.0 Chrome/31.0.1700.0 Safari/537.36"
  92.                                 ]
  93.                             ]);
  94.  
  95.                             $dataImage = $this->curl_get_contents($imageLink);
  96.                             file_put_contents($fullPath . '/' . $hFileName . '.jpg', $dataImage);
  97.  
  98.                             $imagesPathArray[] = [
  99.                                 'path' => '/apartment/' . $hash . '/',
  100.                                 'name' => $hFileName . '.jpg'
  101.                             ];
  102.                         }
  103.  
  104.                         $parser = new Parser([
  105.                             'h1' => $apartmentH1 . ' / ' . $apartmentAreaLocation,
  106.                             'hash' => $hash,
  107.                             'name' => $apartmentTitle,
  108.                             'price' => $apartmentPrice, // On Olxwe cant get phone numbers
  109.                             'external_info' => $apartmentExternalInfo,
  110.                             'contacts' => '', //,
  111.                             'description' => $apartmentText,
  112.                             'site' => $baseUrl,
  113.                             'link_to_object' => $apartmentLink,
  114.                             'status' => Parser::STATUS_ACTIVE
  115.                         ]);
  116.  
  117.                         if ($parser->save()) {
  118.                             foreach ($imagesPathArray as $value) {
  119.                                 $parserImage = new ParserImages();
  120.                                 $parserImage->parser_id = $parser->id;
  121.                                 $parserImage->path = $value['path'];
  122.                                 $parserImage->name = $value['name'];
  123.                                 $parserImage->save();
  124.                             }
  125.                         }
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement