PHP simple implementation spider catch URL

Source: Internet
Author: User

0x01

Spider crawl URL uses the file_get_contents ()/fopen () function, using the regular matching method (seemingly the simplest way, record next add)

0x02

usage:php spider.php www.baidu.com results saved in www.baidu.com file

Code:

-----------------

<?PHP//2015-4-16 @developd//Get URL in Web page to save URL.txt in//usage php spider.php URLif($ARGC!== 2){   Echo"No target...\r\n"; Echo"usage:php spider.php URL \ r \ n"; Echo"Eg:php spider.php www.baidu.com \ r \ n"; Exit(); }Else{    $url=$argv[' 1 '];}if(Empty($url)){    Echo"URL Error \ r \ n"; Exit(); }$filename="$url";$url= "http://".$url;if(!file_exists($filename)){        $file=fopen($filename, "A +"); fclose($file);}$site=substr($url, 0,Strpos($url, '/', 8));$base=substr($url, 0,Strpos($url, '/') +1);$fp=fopen($url, ' R '); while(!feof($fp)){        $contents.=fread($fp, 1024); //Var_dump ($content);}$pattern= "|href=["]? ([^ ' \ "]+) [' \ ']| U;Preg_match_all($pattern,$contents,$REGARR,preg_set_order); for($i= 0;$i<Count($REGARR);$i++){    if(substr($REGARR[$i][1],0,1) = = "/")            $data= "URL". ($i+1). ":".$site.$REGARR[$i][1].Php_eol; Else            $data= "URL". ($i+1). ":".$REGARR[$i][1].Php_eol; $res=file_put_contents($filename,$data,file_append);}if(Empty($res)){    Echo"No urls...\r\n";}Else{    Echo"Get URLS success...\r\n";}fclose($fp);?>

-----------------

PHP simple implementation spider catch URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.