Advertisement
Guest User

Untitled

a guest
Apr 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.16 KB | None | 0 0
  1. <meta charset="utf-8">
  2. <?php
  3.     mb_internal_encoding("UTF-8");
  4.     class Curl {
  5.         public function pfs($url){
  6.             $result = $this->parse($url);
  7.             if(!$result)echo "error parse";
  8.             else{
  9.                 $result = $this->find($result);
  10.                 if(!$result)echo "error find";
  11.                 else{
  12.                     $result = $this->save($result);
  13.                     if(!$result)echo "error save<br>";
  14.                     else echo "Успешное сохранение<br>";
  15.                 }
  16.             }
  17.         }
  18.        
  19.         private function parse($url){
  20.             $curl = curl_init();
  21.            
  22.             curl_setopt($curl, CURLOPT_URL, $url);
  23.             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  24.             curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  25.  
  26.             $result = curl_exec($curl);
  27.             if ($result === false) {
  28.             return false;
  29.             } else {
  30.                 return $result;
  31.             }
  32.         }
  33.        
  34.         private function find($res){
  35.             preg_match_all("#<title>(.*?)</title>#su", "$res", $title);
  36.             $title = $title[1][0];
  37.            
  38.             preg_match_all("#<h1.*?>(.*?)</h1>#su", "$res", $h1);
  39.             $h1 = $h1[1][0];
  40.            
  41.             preg_match_all('#</h1>(.*?)<div\s+id="footer"\s*>#su', "$res", $text);
  42.             $text = $text[1][0];
  43.             $text = preg_replace("#<span.*?>(.*?)</span>#su", "$1", $text);    
  44.             $text = preg_replace('#<p\s+class=".*?">(.*?)</p>#su', "<p>$1</p>", $text);
  45.             $text = preg_replace('#<a.+?href=".*?".*?>(.*?)</a>#su', "$1", $text);     
  46.             $text = preg_replace('#<div.*?>#su', "", $text);   
  47.             $text = preg_replace('#</div>#su', "", $text); 
  48.            
  49.             $result = ['title' => $title, 'h1' => $h1, 'text' => $text];
  50.             return $result;
  51.         }
  52.        
  53.         private function save($res){
  54.             $host = 'localhost';
  55.             $database = 'parse2';
  56.             $user = 'root';
  57.             $password = '';
  58.            
  59.             $link = mysqli_connect($host, $user, $password, $database)
  60.                 or die("Ошибка " . mysqli_error($link));
  61.             mysqli_set_charset($link, "utf-8");
  62.  
  63.             $query = "INSERT INTO parse (title, h1, text)
  64.             VALUES ('".$res['title']."', '".$res['h1']."', '".$res['text']."')";
  65.             $result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));    
  66.            
  67.             mysqli_close($link);
  68.             return true;
  69.         }
  70.     }
  71.    
  72.     $links = [
  73.         '',
  74.         '',
  75.         '',
  76.         '',
  77.         ''
  78.     ];
  79.    
  80.     foreach($links as $el){
  81.         $parse = new Curl;
  82.         $parse->pfs($el);
  83.     }
  84. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement