Advertisement
DVS_studio

miele parser

May 31st, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.75 KB | None | 0 0
  1. <?php
  2.     header('Content-Type: text/html; charset=utf-8');
  3.     include "./simple_html_dom.class.php";
  4.     ob_implicit_flush();
  5.     error_reporting(E_ALL);
  6.     ini_set('display_errors', 1);
  7.     function f() {
  8.         flush();
  9.         ob_flush();
  10.     }
  11.  
  12.     function d($o) {
  13.         echo "<pre>";
  14.         print_r($o);
  15.         echo "</pre>";
  16.         f();
  17.     }
  18.  
  19.     function htmlGet($url) {
  20.         if (strpos($url, "/") === 0)
  21.             $url = "https://www.miele-shop.ru" . $url;
  22.         echo $url;
  23.         f();
  24.         $ch = curl_init($url);
  25.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   // возвращает веб-страницу
  26.         curl_setopt($ch, CURLOPT_HEADER, 0);           // не возвращает заголовки
  27.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);   // переходит по редиректам
  28.         curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36");
  29.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); // таймаут соединения
  30.         curl_setopt($ch, CURLOPT_TIMEOUT, 20);        // таймаут ответа
  31.         curl_setopt($ch, CURLOPT_MAXREDIRS, 10);      // останавливаться после 10-ого редиректа
  32.         curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
  33.         curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
  34.         $content = curl_exec($ch);
  35.         curl_close($ch);
  36.         return $content;
  37.     }
  38.  
  39.     $index = str_get_html(htmlGet("/"));
  40.     foreach ($index->find("#menuCatalog", 0)->find("a") as $index_a) {
  41.         echo $index_a->plaintext . "<br>";
  42.         f();
  43.         $category = str_get_html(htmlGet($index_a->href));
  44.         foreach ($category->find("#categories", 0)->find(".categoryItem") as $category_div) {
  45.             $category_a = $category_div->find(".itemDetails", 0)->find("a", 0);
  46.             echo "&nbsp;" . $category_a->plaintext . "<br>";
  47.             f();
  48.             $sub_category = str_get_html(htmlGet($category_a->href));
  49.             foreach ($sub_category->find("#products", 0)->find(".productItem") as $product_div) {
  50.                 $product_a = $product_div->find(".productModel", 0)->find("a", 0);
  51.                 echo "&nbsp;&nbsp;" . $product_a->plaintext . "<br>";
  52.                 f();
  53.                 $product = str_get_html(htmlGet($product_a->href));
  54.                 $prod["title"] = $product->find("h2", 0)->plaintext;
  55.                 $prod["article"] = $product->find(".description", 0)->find("strong", 0)->plaintext;
  56.                 $prod["price"] = preg_replace("([^\d,])", "", $product->find("#infoline", 0)->find(".bigger", 0)->plaintext);
  57.                 $prod["image"] = $product->find(".imageBig", 0)->find("a", 0)->href;
  58.                 $prod["description"] = $product->find(".descriptionText", 0)->innertext;
  59.                 $chars = $product->find("#characteristicTable", 0);
  60.                 if ($chars) $prod["chars"] = $chars->outertext;
  61.                 d($prod);
  62.                 f();
  63.                 exit;
  64.             }
  65.             exit;
  66.         }
  67.         exit;
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement