PHP/Cron Emailskraber/WebCrawler

Opbygning

En simpel webcrawler der tjekker URL's og gemmer alle links og emails den støder på.

Der er bestemt plads til forbedringer, men skulle bare lige prøve hvad der skulle til :-)

Databasen:

CREATE TABLE IF NOT EXISTS links (
  id int(11) NOT NULL auto_increment,
  url varchar(250) NOT NULL,
  parsed tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (id),
  UNIQUE KEY url (url),
  KEY parsed (parsed)
) ENGINE=MyISAM;

CREATE TABLE IF NOT EXISTS mails (
  id int(11) NOT NULL auto_increment,
  mail varchar(250) NOT NULL,
  PRIMARY KEY  (id),
  UNIQUE KEY mail (mail)
) ENGINE=MyISAM;

--Og en test linie så vi har noget at starte på
INSERT INTO links ('', 'http://jonasholbech.dk/blog/php/emailharvest', 0);

Scriptet:

Kald filen hvad du vil, men i det efterfølgende bliver den refereret til som "spiderCli.php"


#!/usr/bin/php
<?php
ini_set
('error_reporting'2047);
ini_set('display_errors''On');
ini_set('max_execution_time',0);
$connection mysql_connect("localhost""USER""PASS");
mysql_select_db("DB"$connection) or die(mysql_error());
$bad=array(
    
'exe''zip''wma''ico''gif',
    
'jpg''jpeg''png''gz''tar',
    
'ace''rar''pdf''doc''odt',
    
'flv''avi''mpg''mpeg''mp3',
    
'ogg''flac''svg''par''bin',
    
'dmg''fla''swf'
);
function 
addLink($s){
    global 
$bad;
    if(
in_array(getext($s),$bad)){
        return;
    }
    if(
stristr($s'wiki') && stristr($s'edit')){
        return;
    }
    
$s eregi_replace('#.+$'''$s);//fjern interne #name ref's

    
$val=0;
    
mysql_query("INSERT INTO links VALUES ('', '$s', $val)");
}

function 
addEmail($s){
    
mysql_query("INSERT INTO mails VALUES ('', '$s')");
}
function 
checkLine($l$path){//burde fjerne #
    
if(eregi('href=("|\')([^ \(\)"\']+)("|\')'$l$a)){
        
$m=$a[2];
        if(
substr($m04)=='http'){
            
addLink($m);
        } elseif(
substr($m07)=='mailto:'){
            
addEmail(substr($m7));
        } else {
//relative links
            
$pathroot=explode('/'$path);
            
$pathroot=$pathroot[0].'//'.$pathroot[2];
            if(
substr($m01)=='/'){
                
$newPath $pathroot.$m;
                
addLink($newPath);
            } else {
                
$newPath $pathroot.'/'.$m;
                
addLink($newPath);
            }
        }
        
$l str_replace($m''$l);
        
checkLine($l$path);
    } elseif(
eregi('[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})'$l$a)){
        
addEmail($a[0]);
        
$l str_replace($a[0], ''$l);
        
checkLine($l$path);
    }
}
function 
getext($l){
    
$path_info pathinfo($l);
    if(@!
$path_info['extension']){
        return 
$l;
    }
    return 
$path_info['extension'];
}


if(
in_array('-num'$argv)){
  
$count$argv[2];
  
$i=1;
    
$res mysql_query("SELECT url, id FROM links WHERE parsed =0 ORDER BY RAND() LIMIT ".$count);
    while(
$r=mysql_fetch_array($res)){
      if(
$c=@file($r[0])){
          foreach(
$c as $v){
              
checkLine($v$r[0]);
            }
            
mysql_query("UPDATE links SET parsed=1 WHERE id= ".$r[1]);
        } else {
          
mysql_query("UPDATE links SET parsed=4 WHERE id= ".$r[1]);
        }
        echo 
$i.': '.$r[1].' ';
        
$i++;
    }
}
?>

Shell / Cron

Åbn en shell og...

Først skal vores script gøres "eksekverbart":

chmod +x spiderCli.php

Vi tilføjer en linie i cronen
Pas på hvis du kopierer nedenstående, " - " strengen laver sig gerne om???!

EDITOR=gedit crontab -e

Følgende linie tilføjes: (tilpas stien til scriptet, det skal være en linie, knækket her for IE's skyld)

0,30 * * * * /home/holbech/public_html/spider/spiderCli.php -num 10  
>/dev/null 2>&1 # En kommentar til vores crawler

Hvor ti-tallet (10) er antallet af links der skal parses for hvert gennemløb.

Vil du teste scriptet uden cron, så gør det eksekverbart som ovenfor, "cd" hen til scriptet og:

./spiderCli.php -num 1

Gem -> luk -> vent

Hvad mangler?

Mange ting, bla.

  1. Session id's der er i url'en kunne man nok godt pille ud
  2. Det skal være en klasse istedet for det her rod
  3. Bedre håndtering af links der timer ud, 404, 500 etc
  4. Den tager stadig parametre sendt til mailen med (hey, måske er det en feature?) ala mail@example.dk?subject=Test
  5. Der er mange flere filtyper der ikke skal parses

Ich mag diese Website. Es

Ich mag diese Website. Es ist in eine saubere, einfache und elegante Weise entwickelt - aber der Inhalt ist, was hält mich wieder. Bin ich immer meine Botschaften?

Ich mag diese Website. Es

Ich mag diese Website. Es ist in eine saubere, einfache und elegante Weise entwickelt - aber der Inhalt ist, was hält mich wieder. Bin ich immer meine Botschaften?

reply this post

Hello dudes! Welcome to our article submission directories service with the knowledge, skills and the newest ways of submission that permanently deliver results!

answer this post

United kingdom is a famous country and buy essays uk service is the best online. So, just because of that clients opt for it to purchase custom papers.

reply this post

If you try to find locality where you can get resume company here is very super-duper place for you about this topic, which implement examples and gives an occasion to learn how make great CV resumes . But this site is more charismatic, and more effectual.

answer

Searching for premium quality and the most reasonable prices? You can to buy custom papers at professional custom writing firm.

answer

All people at shool are trying to get the PhD degree and they purchase the custom written essays associated with this good post in the custom writing service, and very oft they just demand the topics just about paper writer.

Indsend kommentar

Indholdet af dette felt er privat og bliver ikke vist offentligt.
  • Web- og e-mail-adresser omdannes automatisk til links.
  • Tilladte HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Linjer og afsnit ombrydes automatisk.

Yderligere information om inddataformater