PHP 爬取網(wǎng)頁(yè)的主要方法
主要流程就是獲取整個(gè)網(wǎng)頁(yè),然后正則匹配(關(guān)鍵的)。
PHP抓取頁(yè)面的主要方法,有幾種方法是網(wǎng)上前輩的經(jīng)驗(yàn),現(xiàn)在還沒(méi)有用到的,先存下來(lái)以后試試。
1.file()函數(shù)
2.file_get_contents()函數(shù)
3.fopen()->fread()->fclose()模式
4.curl方式 (本人主要用這個(gè))
5.fsockopen()函數(shù) socket模式
6.插件(如:http://sourceforge.net/projects/snoopy/)
7.file()函數(shù)
<?php //定義url $url='[http://t.qq.com](http://t.qq.com/)';//fiel函數(shù)讀取內(nèi)容數(shù)組 $lines_array=file($url);//拆分?jǐn)?shù)組為字符串 $lines_string=implode('',$lines_array);//輸出內(nèi)容 echo $lines_string;
2.使用file_get_contents方法實(shí)現(xiàn),比較簡(jiǎn)單。
使用file_get_contents和fopen必須空間開(kāi)啟allow_url_fopen。方法:編輯php.ini,設(shè)置 allow_url_fopen = On,allow_url_fopen關(guān)閉時(shí)fopen和file_get_contents都不能打開(kāi)遠(yuǎn)程文件。
$url="[http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml](http://news.sina.com.cn/c/nd/2016-10-23/doc-ifxwztru6951143.shtml)"; $html=file_get_contents($url); //如果出現(xiàn)中文亂碼使用下面代碼` //$getcontent = iconv("gb2312", "utf-8",$html); echo"<textarea style='width:800px;height:600px;'>".$html."</textarea>";
3.fopen()->fread()->fclose()模式 ,目前還沒(méi)用過(guò),看到了就先記下了
<?php //定義url $url='[http://t.qq.com](http://t.qq.com/)';//fopen以二進(jìn)制方式打開(kāi) $handle=fopen($url,"rb");//變量初始化 $lines_string="";//循環(huán)讀取數(shù)據(jù) do{ $data=fread($handle,1024); if(strlen($data)==0) {` break; } $lines_string.=$data; }while(true);//關(guān)閉fopen句柄,釋放資源 fclose($handle);//輸出內(nèi)容 echo $lines_string;
4.使用curl實(shí)現(xiàn)(本人一般使用這個(gè))。
使用curl必須空間開(kāi)啟curl。方法:windows下修改php.ini,將extension=php_curl.dll前面的分號(hào)去掉,而且需 要拷貝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安裝curl擴(kuò)展。
<?php header("Content-Type: text/html;charset=utf-8"); date_default_timezone_set('PRC'); $url = "https://***********ycare";//要爬取的網(wǎng)址 $res = curl_get_contents($url);//curl封裝方法 preg_match_all('/<script>(.*?)<\/script>/',$res,$arr_all);//這個(gè)網(wǎng)頁(yè)中數(shù)據(jù)通過(guò)js包過(guò)來(lái),所以直接抓js就可以 preg_match_all('/"id"\:"(.*?)",/',$arr_all[1][1],$arr1);//從js塊中匹配要的數(shù)據(jù) $list = array_unique($arr1[1]);//(可省)保證不重復(fù) //以下則是同理,循環(huán)則可 for($i=0;$i<=6;$i=$i+2){ $detail_url = 'ht*****em/'.$list[$i]; $detail_res = curl_get_contents($detail_url); preg_match_all('/<script>(.*?)<\/script>/',$detail_res,$arr_detail); preg_match('/"desc"\:"(.*?)",/',$arr_detail[1][1],$arr_content); *** *** *** $ret=curl_post('http://**********cms.php',$result);//此腳本未放在服務(wù)器上,原因大家懂就好哈。 } function curl_get_contents($url,$cookie='',$referer='',$timeout=300,$ishead=0) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1); curl_setopt($curl, CURLOPT_URL,$url); curl_setopt($curl, CURLOPT_TIMEOUT,$timeout); curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'); if($cookie) { curl_setopt( $curl, CURLOPT_COOKIE,$cookie); } if($referer) { curl_setopt ($curl,CURLOPT_REFERER,$referer); } $ssl = substr($url, 0, 8) == "https://" ? TRUE : FALSE; if ($ssl) { curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); } $res = curl_exec($curl); return $res; curl_close($curl); } //curl post數(shù)據(jù)到服務(wù)器 function curl_post($url,$data){ $ch = curl_init(); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST,true); curl_setopt($ch,CURLOPT_POSTFIELDS,$data); $output = curl_exec($ch); curl_close($ch); return $output; } ?>
5.fsockopen()函數(shù) socket模式(沒(méi)用過(guò),以后可以試試)
socket模式能否正確執(zhí)行,也跟服務(wù)器的設(shè)置有關(guān)系,具體可以通過(guò)phpinfo查看服務(wù)器開(kāi)啟了哪些通信協(xié)議
<?php $fp = fsockopen("t.qq.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: t.qq.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); }
6.snoopy插件,最新版本是Snoopy-1.2.4.zip Last Update: 2013-05-30,推薦大家使用
使用網(wǎng)上非常流行的snoopy來(lái)進(jìn)行采集,這是一個(gè)非常強(qiáng)大的采集插件,并且它的使用非常方便,你也可以在里面設(shè)置agent來(lái)模擬瀏覽器信息。
說(shuō)明:設(shè)置agent是在 Snoopy.class.php 文件的第45行,請(qǐng)?jiān)谠撐募兴阉?“var 公式輸入有誤_SERVER['HTTP_USER_AGENT']; 可以得到瀏覽器信息,將echo出來(lái)的內(nèi)容復(fù)制到agent里面就可以了。
<?php //引入snoopy的類(lèi)文件 require('Snoopy.class.php'); //初始化snoopy類(lèi) $snoopy=new Snoopy; $url="[http://t.qq.com](http://t.qq.com/)"; //開(kāi)始采集內(nèi)容` $snoopy->fetch($url); //保存采集內(nèi)容到$lines_string $lines_string=$snoopy->results; //輸出內(nèi)容,嘿嘿,大家也可以保存在自己的服務(wù)器上 echo $lines_string;
總結(jié)
以上所述是小編給大家介紹的PHP 爬取網(wǎng)頁(yè)的主要方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- php實(shí)現(xiàn)的支付寶網(wǎng)頁(yè)支付功能示例【基于TP5框架】
- Windows平臺(tái)PHP+IECapt實(shí)現(xiàn)網(wǎng)頁(yè)批量截圖并創(chuàng)建縮略圖功能詳解
- php實(shí)現(xiàn)網(wǎng)頁(yè)上一頁(yè)下一頁(yè)翻頁(yè)過(guò)程詳解
- 實(shí)例分析基于PHP微信網(wǎng)頁(yè)獲取用戶信息
- php實(shí)現(xiàn)網(wǎng)頁(yè)常見(jiàn)文件上傳功能
- php中抓取網(wǎng)頁(yè)內(nèi)容的實(shí)例詳解
- php編程實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)頁(yè)版計(jì)算器功能示例
- PHP網(wǎng)頁(yè)緩存技術(shù)優(yōu)點(diǎn)及代碼實(shí)例
相關(guān)文章
thinkPHP基于ajax實(shí)現(xiàn)的菜單與分頁(yè)示例
這篇文章主要介紹了thinkPHP基于ajax實(shí)現(xiàn)的菜單與分頁(yè),結(jié)合實(shí)例形式分析了thinkPHP基于ajax無(wú)刷新交互實(shí)現(xiàn)菜單及分頁(yè)功能的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-07-07解決PHP里大量數(shù)據(jù)循環(huán)時(shí)內(nèi)存耗盡的方法
錯(cuò)誤信息提示最大內(nèi)存已經(jīng)耗盡,該如何解決呢?下面小編給大家解決PHP里大量數(shù)據(jù)循環(huán)時(shí)內(nèi)存耗盡的問(wèn)題,需要的朋友可以參考下2015-10-10Zend Framework教程之Application和Bootstrap用法詳解
這篇文章主要介紹了Zend Framework教程之Application和Bootstrap用法,結(jié)合實(shí)例形式詳細(xì)分析了Application和Bootstrap的功能,使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03