Advertisement
Guest User

task1

a guest
Mar 1st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. error_reporting(E_ALL);
  5.  
  6.  
  7. require 'phpQuery.php';
  8.  
  9. $url = 'http://theory.phphtml.net/exercises/advanced/php/parsing/zadachi-na-poetapnyj-parsing-i-metod-pauka/1/index.php';
  10. $prefix = 'http://theory.phphtml.net/exercises/advanced/php/parsing/zadachi-na-poetapnyj-parsing-i-metod-pauka/1/';
  11. $result = connect($url);
  12.  
  13. $pq = phpQuery::newDocument($result);
  14. $links = $pq->find('#menu a');
  15.  
  16. foreach($links as $link){
  17. $link = pq($link);
  18. $href = $link->attr('href');
  19. $url = $prefix.$href;
  20. $contetntPage = connect($url);
  21. $pq = phpQuery::newDocument($contetntPage);
  22. $title = $pq->find('title')->text();
  23. $content = $pq->find('#content p')->text();
  24. addContent($url, $title, $content);
  25.  
  26. }
  27.  
  28.  
  29. function addContent($url, $title, $content)
  30. {
  31.  
  32. $host = 'localhost';
  33. $user = 'root';
  34. $password = '';
  35. $db_name = 'phpquery';
  36. $connect = mysqli_connect($host, $user, $password, $db_name) or die(mysqli_error($link));
  37. $query = "INSERT INTO page (id, title, content, url) VALUES (null, '".$title."', '".$content."','".$url."')";
  38. $result = mysqli_query($connect, $query);
  39.  
  40. return $result;
  41.  
  42. }
  43.  
  44.  
  45. function connect($url)
  46. {
  47.  
  48. $curl = curl_init();
  49. curl_setopt($curl, CURLOPT_URL, $url);
  50. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  51. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  52.  
  53. $result = curl_exec($curl);
  54. return $result;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement