實現這個功能的步驟:
首先打開百度百科,在搜索框輸入“php”關鍵詞,得到搜索列表,一般都是10條;
然後使用火狐的Firebug分析百度列表的內容組成,主要是html標籤,發現去向百科內容的連接的格式都是http://baike.baidu.com/view/5721060.htm,於是使用正則匹配鏈接,得到10條鏈接;
之後把鏈接交由curl()批處理函數(自定義)處理,得到結果頁面的內容;
然後在對內容頁面進行分析,找出你想要的內容,本程序抓取的是標題以及簡介;
最後,把結果呈現在tabel標籤中。
此外,我還把抓取的數據寫入了數據庫中。
所有的代碼如下:
程序名爲curl.php的代碼:
<?php require 'func.curl.php'; require 'func.mysql.php'; //CURL if (isset($_GET['action'])){ $_key=$_POST['key']; $_ch=curl_init(); curl_setopt($_ch,CURLOPT_URL,"http://baike.baidu.com/search?word=".$_key); curl_setopt($_ch,CURLOPT_RETURNTRANSFER,1); $_output=curl_exec($_ch); curl_close($_ch); if(preg_match_all('/(http\:\/\/baike\.baidu\.com.*)\"\s/',$_output,$_url)){ $_res=curl($_url[1]); $_title=array(); $_content=array(); for ($_i=0;$_i<count($_res);$_i++){ if (preg_match_all('/lemmaTitleH1\"\>(.*?)\s*\<\/div\>/',$_res[$_i],$_arr)){ //標題 $_title[$_i]=strip_tags($_arr[1][0]); if (preg_match_all('/para\"\>(.*?)\s*\<\/div\>/',$_res[$_i],$_arr)){ //內容 $_content[$_i][0]=strip_tags($_arr[1][0]); $_content[$_i][1]=strip_tags($_arr[1][1]); //插入數據庫 inserInfo($_title[$_i],$_content[$_i][0],$_content[$_i][1],$_key); } } } }else{ exit('無匹配結果'); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf8" /> <title>模擬搜索引擎</title> <link type="text/css" rel="stylesheet" href="curl.css"/> </head> <body> <div id="header"> <form method="post" action="?action=search"> <input type="text" name="key" value="請輸入關鍵字" class="text"/> <input type="submit" name="submit" value="搜索" class="submit"/> </form> </div> <div id="content"> <table> <?php for ($i=0;$i<count($_title);$i++){?> <tr><td class="title"><a href="<?php echo $_url[1][$i];?>" target="_blank"><?php echo $_title[$i];?></a></td></tr> <tr><td class="content"><?php echo $_content[$i][0]?></td></tr> <?php }?> </table> </div> <script type="text/javascript"> var key=document.getElementsByName('key')[0]; key.onfocus=function(){ this.value=''; }; key.onblur=function(){ if(this.value==""){ this.value="請輸入關鍵字"; } }; </script> </body> </html>
func.curl.php的代碼:
<?php //curl批處理函數 function curl($connomains){ $mh = curl_multi_init(); foreach ($connomains as $i => $url) { $conn[$i] = curl_init("$url"); curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1); curl_multi_add_handle ($mh,$conn[$i]); } // start performing the request do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active and $mrc == CURLM_OK) { // wait for network if (curl_multi_select($mh) != -1) { // pull in any new data, or at least handle timeouts do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } } // retrieve data foreach ($connomains as $i => $url) { if (($err = curl_error($conn[$i])) == '') { $res[$i]=curl_multi_getcontent($conn[$i]); } else { print "Curl error on handle $i: $err\n"; } curl_multi_remove_handle($mh,$conn[$i]); curl_close($conn[$i]); } curl_multi_close($mh); return $res; } ?>
func.mysql.php的代碼爲:
<?php function inserInfo($_title,$_content,$_content_extra,$_keywords){ $_mysql=mysql_connect('localhost','root','123') or die('數據庫連接錯誤'); mysql_select_db('baike',$_mysql); mysql_query('set names utf8'); mysql_query("INSERT INTO info (title,content,content_extra,keywords) VALUES( '$_title', '$_content', '$_content_extra', '$_keywords' ) "); mysql_close(); } ?>