使用CURL構(gòu)建爬蟲,抓取百度百科內(nèi)容

實(shí)現(xiàn)這個功能的步驟:

創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為商南等服務(wù)建站,商南等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為商南企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

首先打開百度百科,在搜索框輸入“php”關(guān)鍵詞,得到搜索列表,一般都是10條;

然后使用火狐的Firebug分析百度列表的內(nèi)容組成,主要是html標(biāo)簽,發(fā)現(xiàn)去向百科內(nèi)容的連接的格式都是http://baike.baidu.com/view/5721060.htm,于是使用正則匹配鏈接,得到10條鏈接;

之后把鏈接交由curl()批處理函數(shù)(自定義)處理,得到結(jié)果頁面的內(nèi)容;

然后在對內(nèi)容頁面進(jìn)行分析,找出你想要的內(nèi)容,本程序抓取的是標(biāo)題以及簡介;

最后,把結(jié)果呈現(xiàn)在tabel標(biāo)簽中。

此外,我還把抓取的數(shù)據(jù)寫入了數(shù)據(jù)庫中。

所有的代碼如下:

程序名為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)){
                    //標(biāo)題
                    $_title[$_i]=strip_tags($_arr[1][0]);
                    if (preg_match_all('/para\"\>(.*?)\s*\<\/div\>/',$_res[$_i],$_arr)){
                        //內(nèi)容
                        $_content[$_i][0]=strip_tags($_arr[1][0]);
                        $_content[$_i][1]=strip_tags($_arr[1][1]);
                        //插入數(shù)據(jù)庫
                        inserInfo($_title[$_i],$_content[$_i][0],$_content[$_i][1],$_key);
                    }
                }
            }
        }else{
            exit('無匹配結(jié)果');
        }
    }
?>   
<!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="請輸入關(guān)鍵字" 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="請輸入關(guān)鍵字";
                }
        };
</script>
</body>
</html>

func.curl.php的代碼:

<?php
//curl批處理函數(shù)
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('數(shù)據(jù)庫連接錯誤');
        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();           
}
?>

名稱欄目:使用CURL構(gòu)建爬蟲,抓取百度百科內(nèi)容
文章源于:http://bm7419.com/article16/igsigg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、自適應(yīng)網(wǎng)站網(wǎng)站導(dǎo)航、云服務(wù)器、外貿(mào)建站、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)