Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <meta charset="utf-8">
- <?php
- mb_internal_encoding("UTF-8");
- class Curl {
- public function pfs($url){
- $result = $this->parse($url);
- if(!$result)echo "error parse";
- else{
- $result = $this->find($result);
- if(!$result)echo "error find";
- else{
- $result = $this->save($result);
- if(!$result)echo "error save<br>";
- else echo "Успешное сохранение<br>";
- }
- }
- }
- private function parse($url){
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
- $result = curl_exec($curl);
- if ($result === false) {
- return false;
- } else {
- return $result;
- }
- }
- private function find($res){
- preg_match_all("#<title>(.*?)</title>#su", "$res", $title);
- $title = $title[1][0];
- preg_match_all("#<h1.*?>(.*?)</h1>#su", "$res", $h1);
- $h1 = $h1[1][0];
- preg_match_all('#</h1>(.*?)<div\s+id="footer"\s*>#su', "$res", $text);
- $text = $text[1][0];
- $text = preg_replace("#<span.*?>(.*?)</span>#su", "$1", $text);
- $text = preg_replace('#<p\s+class=".*?">(.*?)</p>#su', "<p>$1</p>", $text);
- $text = preg_replace('#<a.+?href=".*?".*?>(.*?)</a>#su', "$1", $text);
- $text = preg_replace('#<div.*?>#su', "", $text);
- $text = preg_replace('#</div>#su', "", $text);
- $result = ['title' => $title, 'h1' => $h1, 'text' => $text];
- return $result;
- }
- private function save($res){
- $host = 'localhost';
- $database = 'parse2';
- $user = 'root';
- $password = '';
- $link = mysqli_connect($host, $user, $password, $database)
- or die("Ошибка " . mysqli_error($link));
- mysqli_set_charset($link, "utf-8");
- $query = "INSERT INTO parse (title, h1, text)
- VALUES ('".$res['title']."', '".$res['h1']."', '".$res['text']."')";
- $result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
- mysqli_close($link);
- return true;
- }
- }
- $links = [
- '',
- '',
- '',
- '',
- ''
- ];
- foreach($links as $el){
- $parse = new Curl;
- $parse->pfs($el);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement