PHP抓取及分析網頁的方法詳解,php抓取分析詳解_PHP教程

來源:互聯網
上載者:User

PHP抓取及分析網頁的方法詳解,php抓取分析詳解


本文執行個體講述了PHP抓取及分析網頁的方法。分享給大家供大家參考,具體如下:

抓取和分析一個檔案是非常簡單的事。這個教程將通過一個例子帶領你一步一步地去實現它。讓我們開始吧!

首先,我首必須決定我們將抓取的URL地址。可以通過在指令碼中設定或通過$QUERY_STRING傳遞。為了簡單起見,讓我們將變數直接設在指令碼中。

<?php$url = 'http://www.php.net';?>

第二步,我們抓取指定檔案,並且通過file()函數將它存在一個數組裡。

<?php$url = 'http://www.php.net';$lines_array = file($url);?>

好了,現在在數組裡已經有了檔案了。但是,我們想分析的文本可能不全在一行裡面。為瞭解決這個檔案,我們可以簡單地將數組$lines_array轉化成一個字串。我們可以使用implode(x,y)函數來實現它。如果在後面你想用explode(將字串變數數組),將x設成"|"或"!"或其它類似的分隔字元可能會更好。但是出於我們的目的,最好將x設成空格。y是另一個必要的參數,因為它是你想用implode()處理的數組。

<?php$url = 'http://www.php.net';$lines_array = file($url);$lines_string = implode('', $lines_array);?>

現在,抓取工作就做完了,下面該進行分析了。出於這個例子的目的,我們想得到在到之間的所有東西。為了分析出字串,我們還需要叫做正規運算式的東西。

<?php$url = 'http://www.php.net';$lines_array = file($url);$lines_string = implode('', $lines_array);eregi("(.*)", $lines_string, $head);?>

讓我們看一下代碼。正如你所見,eregi()函數按下面的格式執行:

eregi("(.*)", $lines_string, $head);

"(.*)"表示所有東西,可以解釋為,"分析在和間的所以東西"。$lines_string是我們正在分析的字串,$head是分析後的結果存放的數組。

最後,我們可以輸資料。因為僅在和間存在一個執行個體,我們可以安全的假設數組中僅存在著一個元素,而且就是我們想要的。讓我們把它列印出來吧。

<?php$url = 'http://www.php.net';$lines_array = file($url);$lines_string = implode('', $lines_array); eregi("(.*)", $lines_string, $head);echo $head[0];?>

這就是全部的代碼了。

<?php//擷取所有內容url儲存到檔案function get_index ( $save_file , $prefix = "index_" ){   $count = 68 ;   $i = 1 ;  if ( file_exists ( $save_file )) @ unlink ( $save_file );   $fp = fopen ( $save_file , "a+" ) or die( "Open " . $save_file . " failed" );  while( $i < $count ){     $url = $prefix . $i . ".htm" ;    echo "Get " . $url . "..." ;     $url_str = get_content_url ( get_url ( $url ));    echo " OK/n" ;     fwrite ( $fp , $url_str );    ++ $i ;  }   fclose ( $fp );}//擷取目標多媒體對象function get_object ( $url_file , $save_file , $split = "|--:**:--|" ){  if (! file_exists ( $url_file )) die( $url_file . " not exist" );   $file_arr = file ( $url_file );  if (! is_array ( $file_arr ) || empty( $file_arr )) die( $url_file . " not content" );   $url_arr = array_unique ( $file_arr );  if ( file_exists ( $save_file )) @ unlink ( $save_file );   $fp = fopen ( $save_file , "a+" ) or die( "Open save file " . $save_file . " failed" );  foreach( $url_arr as $url ){    if (empty( $url )) continue;    echo "Get " . $url . "..." ;     $html_str = get_url ( $url );    echo $html_str ;    echo $url ;    exit;     $obj_str = get_content_object ( $html_str );    echo " OK/n" ;     fwrite ( $fp , $obj_str );  }   fclose ( $fp );}//遍曆目錄擷取檔案內容function get_dir ( $save_file , $dir ){   $dp = opendir ( $dir );  if ( file_exists ( $save_file )) @ unlink ( $save_file );   $fp = fopen ( $save_file , "a+" ) or die( "Open save file " . $save_file . " failed" );  while(( $file = readdir ( $dp )) != false ){    if ( $file != "." && $file != ".." ){      echo "Read file " . $file . "..." ;       $file_content = file_get_contents ( $dir . $file );       $obj_str = get_content_object ( $file_content );      echo " OK/n" ;       fwrite ( $fp , $obj_str );    }  }   fclose ( $fp );}//擷取指定url內容function get_url ( $url ){   $reg = '/^http:////[^//].+$/' ;  if (! preg_match ( $reg , $url )) die( $url . " invalid" );   $fp = fopen ( $url , "r" ) or die( "Open url: " . $url . " failed." );  while( $fc = fread ( $fp , 8192 )){     $content .= $fc ;  }   fclose ( $fp );  if (empty( $content )){    die( "Get url: " . $url . " content failed." );  }  return $content ;}//使用socket擷取指定網頁function get_content_by_socket ( $url , $host ){   $fp = fsockopen ( $host , 80 ) or die( "Open " . $url . " failed" );   $header = "GET /" . $url . " HTTP/1.1/r/n" ;   $header .= "Accept: */*/r/n" ;   $header .= "Accept-Language: zh-cn/r/n" ;   $header .= "Accept-Encoding: gzip, deflate/r/n" ;   $header .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; InfoPath.1; .NET CLR 2.0.50727)/r/n" ;   $header .= "Host: " . $host . "/r/n" ;   $header .= "Connection: Keep-Alive/r/n" ;   //$header .= "Cookie: cnzz02=2; rtime=1; ltime=1148456424859; cnzz_eid=56601755-/r/n/r/n";   $header .= "Connection: Close/r/n/r/n" ;   fwrite ( $fp , $header );   while (! feof ( $fp )) {     $contents .= fgets ( $fp , 8192 );   }   fclose ( $fp );   return $contents ;}//擷取指定內容裡的urlfunction get_content_url ( $host_url , $file_contents ){   //$reg = '/^(#|JavaScript.*?|ftp:////.+|http:////.+|.*?href.*?|play.*?|index.*?|.*?asp)+$/i';   //$reg = '/^(down.*?/.html|/d+_/d+/.htm.*?)$/i';   $rex = "/([hH][rR][eE][Ff])/s*=/s*['/"]*([^>'/"/s]+)[/"'>]*/s*/i" ;   $reg = '/^(down.*?/.html)$/i' ;   preg_match_all ( $rex , $file_contents , $r );   $result = "" ; //array();   foreach( $r as $c ){    if ( is_array ( $c )){      foreach( $c as $d ){        if ( preg_match ( $reg , $d )){ $result .= $host_url . $d . "/n" ; }      }    }  }  return $result ;}//擷取指定內容中的多媒體檔案function get_content_object ( $str , $split = "|--:**:--|" ){   $regx = "/href/s*=/s*['/"]*([^>'/"/s]+)[/"'>]*/s*(.*?)/i" ;   preg_match_all ( $regx , $str , $result );  if ( count ( $result ) == 3 ){     $result [ 2 ] = str_replace ( "多媒體: " , "" , $result [ 2 ]);     $result [ 2 ] = str_replace ( " " , "" , $result [ 2 ]);     $result = $result [ 1 ][ 0 ] . $split . $result [ 2 ][ 0 ] . "/n" ;  }  return $result ;}?>

更多關於PHP相關內容感興趣的讀者可查看本站專題:《phpRegex用法總結》、《PHP+ajax技巧與應用小結》、《PHP運算與運算子用法總結》、《PHP網路編程技巧總結》、《PHP基本文法入門教程》、《php操作office文檔技巧總結(包括word,excel,access,ppt)》、《php日期與時間用法總結》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家PHP程式設計有所協助。

http://www.bkjia.com/PHPjc/1123830.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1123830.htmlTechArticlePHP抓取及分析網頁的方法詳解,php抓取分析詳解 本文執行個體講述了PHP抓取及分析網頁的方法。分享給大家供大家參考,具體如下: 抓取和分...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.