Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Złączone dane
SN@JPER^
post 24.11.2017, 17:44:49
Post #1





Grupa: Zarejestrowani
Postów: 266
Pomógł: 0
Dołączył: 4.01.2007
Skąd: Szczecin

Ostrzeżenie: (0%)
-----


Napisałem sobie taką oto klasę:

  1.  
  2. <?php
  3.  
  4. class Scrapper{
  5.  
  6. public $url;
  7. private $data;
  8. private $dataAfter;
  9. private $doc;
  10. private $xpath;
  11. private $ch;
  12.  
  13. function __construct($url){
  14.  
  15. if (preg_match('/^http/', $url)) {
  16.  
  17. libxml_use_internal_errors(true);
  18.  
  19. $this->url = $url;
  20. $this->data = $this->curl($this->url);
  21.  
  22.  
  23. $this->doc = new \DOMDocument();
  24. $this->doc->loadHTML($this->data);
  25.  
  26. $this->xpath = new DOMXPath($this->doc);
  27.  
  28. }
  29. }
  30.  
  31. public function queryTag($query){
  32.  
  33. if(!empty($query)){
  34.  
  35. $this->data = $this->xpath->query($query);
  36.  
  37. return $this;
  38. }
  39. }
  40.  
  41. public function getData($noHTML = false, $removeAttribute = false){
  42.  
  43. foreach ($this->data as $dataNodes){
  44.  
  45. if($removeAttribute === true) {
  46. $dataNodes->removeAttribute('style');
  47. $dataNodes->removeAttribute('class');
  48. $dataNodes->removeAttribute('id');
  49. }
  50.  
  51. if($noHTML === true){
  52. $this->dataAfter .= $dataNodes->nodeValue;
  53. }else{
  54. $this->dataAfter .= $dataNodes->ownerDocument->saveHTML($dataNodes);
  55. }
  56.  
  57. }
  58.  
  59. return $this->dataAfter;
  60. }
  61.  
  62. private function curl($url){
  63. if(!empty($url)) {
  64.  
  65. $options = Array(
  66. CURLOPT_RETURNTRANSFER => TRUE, // Setting cURL's option to return the webpage data
  67. CURLOPT_FOLLOWLOCATION => TRUE, // Setting cURL to follow 'location' HTTP headers
  68. CURLOPT_AUTOREFERER => TRUE, // Automatically set the referer where following 'location' HTTP headers
  69. CURLOPT_CONNECTTIMEOUT => 120, // Setting the amount of time (in seconds) before the request times out
  70. CURLOPT_TIMEOUT => 120, // Setting the maximum amount of time for cURL to execute queries
  71. CURLOPT_MAXREDIRS => 10, // Setting the maximum number of redirections to follow
  72. CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8", // Setting the useragent
  73. CURLOPT_URL => $this->url, // Setting cURL's URL option with the $url variable passed into the function
  74. );
  75.  
  76. $this->ch = curl_init();
  77. curl_setopt_array($this->ch, $options);
  78. $this->data = curl_exec($this->ch);
  79.  
  80. return $this->data;
  81. }
  82. }
  83.  
  84. function __destruct(){
  85.  
  86. curl_close($this->ch);
  87.  
  88. }
  89.  
  90. }
  91.  
  92.  
  93. $class = new \Scrapper('http://www.....');
  94.  
  95. $pic = $class->queryTag('//div[@id="left"]//img[@class="pic"]/@src')->getData();
  96. $title = $class->queryTag('//div[@id="left"]//h2')->getData(true);
  97. $text = $class->queryTag('//div[@id="left"]/p | //center')->getData(false, true);
  98.  
  99. echo $title;
  100. echo '<hr>';
  101. echo $pic;
  102. echo '<hr>';
  103. echo $text;
  104. echo '<hr>';
  105.  


Po wywołaniu tej klasy, przypisuję do każdej zmiennej szukanej wartości - zdjęcie, tytuł i treść.

Niestety tytuł zawiera również ciąg URL obrazka, natomiast tekst zawiera dodatkowo obrazek oraz tytuł. Gdzie robię błąd? Jak to oddzielić?

Jednocześnie proszę o sugestię co mogę poprawić w samej klasie.

Ten post edytował SN@JPER^ 24.11.2017, 17:47:16
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 25.04.2024 - 09:48