Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: pająk
Forum PHP.pl > Forum > PHP > Object-oriented programming
Apo
Witam
Zaczełem pisać skrypt który będzie miała za zadanie odwiedzanie stron www oraz pobieranie z nich adresów email i zapisanie ich do pliku. Kod:

  1. <?php
  2.  
  3. class spider {
  4. private $emaile = array();
  5. private $adresy = array();
  6. private $file = 'baza.txt';
  7.  
  8. public function __construct( $url )
  9. {
  10. $this->check_urls( $url );
  11. }
  12.  
  13. private function check_urls( $url )
  14. {
  15. if(!$dane = @file_get_contents($url))
  16.  {
  17.  echo 'Strona nie istnieje';
  18.  exit;
  19.  }
  20. else
  21. {
  22. $this->regexp_www( $dane );
  23. }
  24. }
  25.  
  26. public function check_email()
  27. {
  28.  foreach($this->adresy[1] as $www)
  29. {
  30. $this->regexp_email( @file_get_contents('http://'.$www) );
  31. }
  32. $this->write();
  33. }
  34.  
  35. private function write()
  36. {
  37. $a = 0;
  38. $hand = fopen($this->file, 'a');
  39. flock($hand, LOCK_EX);
  40. foreach($this->emaile as $email)
  41. {
  42. if(!empty($email[0]))
  43. {
  44. foreach($email[0] as $right)
  45. {
  46. $a++;
  47. fwrite($hand, $right."\n");
  48. }
  49. }
  50. }
  51. flock($hand, LOCK_UN);
  52. fclose($hand);
  53. echo $a;
  54. }
  55.  
  56. private function regexp_email( &$dane )
  57. {
  58. return preg_match_all('#([\w\.]+)@{1}([\w\.]){1,}\.([a-zA-Z]){2,4}#si', $dane, $this->emaile[]);
  59. }
  60.  
  61. private function regexp_www( &$dane )
  62. {
  63. return preg_match_all('#<a href="http:\/\/(.*?)"#si', $dane, $this->adresy);
  64. }
  65. }
  66.  
  67. $spider = new spider($_GET['adres']);
  68. $spider->check_email();
  69. ?>


No i jak widać skrypt pobiera adres 1 strony z urla. Następnie wchodzi na tą strone i pobiera wszystkie adresy www które pasują do regexp i gdy juz je pobierze i zapisze w składowej to otwiera tą strone i wyciąga z niej emaile.

No i mój problem jest taki że nie wiem jak to zapętlić. Aby skrypt wchodził na strone, zczytywał z niej adresy potem je otwierał i następnie zczytywał adresy stron oraz email i tak wkórło powiedzmy do while(count($this->adresy) < 100). ;/ sad.gif
LBO
poczytaj o rekurencji
ActivePlayer
[ot]a potem dostaje 10 spamow dziennie[/ot]
sobstel
Cytat(ActivePlayer @ 2006-03-12 00:00:08)
[ot]a potem dostaje 10 spamow dziennie[/ot]

[ot]polecam wrzucić na stronę truciznę z http://polish.spampoison.com/[/ot]

(hmmm, to chyba nie do końca off topic)
Vomit
Sopel, tylko, ze taką "trucizne" bardzo łatwo obejsc winksmiley.jpg
sobstel
Cytat(Vomit @ 2006-03-12 11:20:38)
Sopel, tylko, ze taką "trucizne" bardzo łatwo obejsc winksmiley.jpg

tak, tylko trzeba o niej wiedzieć ;-)
Vomit
  1. <?php
  2.  
  3.  
  4.  
  5.  
  6. class Bot
  7. {
  8. const BOT_NAME = 'SeekerDodger';
  9. const BOT_VER = 0.2;
  10. /**
  11.  * Tablica z adresami do odwiedzenia
  12.  * @var array
  13.  */
  14. private $aLinks = array();
  15. /**
  16.  * Tablica ze znalezionymi mailami
  17.  * @var array
  18.  */
  19. private $aMails = array();
  20.  
  21. /**
  22.  * Konstruktor
  23.  * @param string, array adres wyjsciowy
  24.  * @param int ilosc adresow do przeszukania
  25.  */
  26. public function __construct( $adress, $ilosc = 10, $mail_s = 0 )
  27. {
  28. if ( is_array($adress) )
  29. {
  30. foreach( $adress as $line )
  31. {
  32. if ( !in_array($line,$this->aLinks) )
  33. {
  34. $this->aLinks[] = $line;
  35. }
  36. }
  37. }
  38. elseif( is_string($adress) )
  39. {
  40. $this->aLinks[] = $adress;
  41. }
  42.  
  43. for( $i = 0; $i <= $ilosc; $i++ )
  44. {
  45. $string = @file_get_contents($this->aLinks[$i]);
  46. if ( $string !== FALSE )
  47. {
  48. preg_match_all('#<a(.*?)href="(.*?)"(.*?)>#si',$string,$matches);
  49. foreach ( $matches[2] as $link )
  50. {
  51. if ( !ereg('http://([-_\.a-zA-Z0-9]+)\.([-_a-zA-Z0-9?=]+)',$link) )
  52. {
  53. if ( !ereg('^(callto|gg|mailto|jid)+',$link) )
  54. {
  55. $url = $this->TemporaryURL($this->aLinks[$i]);
  56. if ( !in_array($url . $link,$this->aLinks) )
  57. {
  58. $this->aLinks[] = $url . $link;
  59. }
  60. }
  61. }
  62. else
  63. {
  64. if ( !in_array($link,$this->aLinks) )
  65. {
  66. $this->aLinks[] = $link;
  67. }
  68.  
  69. }
  70. }
  71.  
  72. if ( $mail_s == 1 )
  73. {
  74. $this->Mails($string);
  75. }
  76.  
  77. }
  78. }
  79.  
  80. }
  81.  
  82. public function ShowLinks()
  83. {
  84. echo '<pre>';
  85. print_r($this->aLinks);
  86. echo '</pre>';
  87. }
  88.  
  89.  
  90. public function ShowMails()
  91. {
  92. echo '<pre>';
  93. print_r($this->aMails);
  94. echo '</pre>';
  95. }
  96.  
  97.  
  98. /**
  99.  * Przepisuje wyszukane maile z tablicy do pliku
  100.  * @param string
  101.  */
  102. public function SaveLinksToFile( $filename )
  103. {
  104. $fp = fopen($filename,'a');
  105. foreach ( $this->aLinks as $Line )
  106. {
  107. fwrite($fp,$Line . "\r\n");
  108. }
  109. fclose($fp);
  110. }
  111.  
  112. /**
  113.  * Przepisuje wyszukane maile z tablicy do pliku
  114.  * @param string
  115.  */
  116. public function SaveMailsToFile( $filename )
  117. {
  118. $fp = fopen($filename,'a');
  119. foreach ( $this->aMails as $Line )
  120. {
  121. fwrite($fp,$Line . "\r\n");
  122. }
  123. fclose($fp);
  124. }
  125.  
  126. /**
  127.  * Szuka maili w podanym tekscie
  128.  * @param string
  129.  */
  130. private function Mails( $string )
  131. {
  132. preg_match_all('#[-a-zA-Z_.]+(?:@|\[at\]|\(at\))[-_a-zA-Z.]+(?:\.|\[dot\]|\(dot\))[-_a-zA-Z.]+#',$string,$matches);
  133.  
  134. $a1 = array('[at]','(at)','[dot]','(dot)');
  135. $a2 = array('@','@','.','.');
  136.  
  137. foreach ( $matches[0] as $mail )
  138. {
  139. $mail = str_replace($a1,$a2,$mail);
  140.  
  141. if ( !in_array($mail,$this->aMails) )
  142. {
  143. $this->aMails[] = $mail;
  144. }
  145. }
  146. }
  147.  
  148. /**
  149.  * Ustala adres strony
  150.  * @param string adres do konwersji
  151.  * @return string adres przekonwertowany
  152.  */
  153. private function TemporaryURL( $url )
  154. {
  155. $url = parse_url($url);
  156. $url = $url['scheme'] . '://' . $url['host'] . '/';
  157. return $url;
  158. }
  159.  
  160. }
  161.  
  162.  
  163. $bot = new Bot('http://forum.dogs.pl/misc.php?action=list&order=regdate&page=2', 10, 1);
  164. $bot -> ShowLinks();
  165. $bot -> ShowMails();
  166.  
  167. ?>


Niedawno takie cos skleiłem. Wyszuka takze maile z [dot](dot) zamiast kropki i [at](at) zamiast malpy.

Mozemy ustawic aby bot nie szukał maili (zmienna $mail_s = 0 )Nie zrobilem mozliwosci aby bot działał w nieskonczonosc (chodz to bardzo łatwo dorobic).
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.