其實抓ajax異步內容的頁面和抓普通的頁面區別不大。ajax只不過是做了一次異步的http請求,只要使用firebug類似的工具,找到請求的後端服務url和傳值的參數,然後對該url傳遞參數進行抓取即可。
利用Firebug的網絡工具
如果抓去的是頁面,則內容中沒有顯示的數據,是一堆JS代碼。
Code
$cookie_file=tempnam('./temp','cookie'); $ch = curl_init(); $url1 = "http://www.cdut.edu.cn/default.html"; curl_setopt($ch,CURLOPT_URL,$url1); curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1); curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch, CURLOPT_ENCODING ,'gzip'); //加入gzip解析 //設置連接結束後保存cookie信息的文件 curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file); $content=curl_exec($ch); curl_close($ch); $ch3 = curl_init(); $url3 = "http://www.cdut.edu.cn/xww/dwr/call/plaincall/portalAjax.getNewsXml.dwr"; $curlPost = "callCount=1&page=/xww/type/1000020118.html&httpSessionId=12A9B726E6A2D4D3B09DE7952B2F282C&scriptSessionId=295315B4B4141B09DA888D3A3ADB8FAA658&c0-scriptName=portalAjax&c0-methodName=getNewsXml&c0-id=0&c0-param0=string:10000201&c0-param1=string:1000020118&c0-param2=string:news_&c0-param3=number:5969&c0-param4=number:1&c0-param5=null:null&c0-param6=null:null&batchId=0"; curl_setopt($ch3,CURLOPT_URL,$url3); curl_setopt($ch3,CURLOPT_POST,1); curl_setopt($ch3,CURLOPT_POSTFIELDS,$curlPost); //設置連接結束後保存cookie信息的文件 curl_setopt($ch3,CURLOPT_COOKIEFILE,$cookie_file); $content1=curl_exec($ch3); curl_close($ch3);
剛寫的。希望有用
<?php $binfo =array('Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; InfoPath.2; AskTbPTV/5.17.0.25589; Alexa Toolbar)','Mozilla/5.0 (Windows NT 5.1; rv:22.0) Gecko/20100101 Firefox/22.0','Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; Alexa Toolbar)','Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1; SV1)',$_SERVER['HTTP_USER_AGENT']);//218.242.124.16*//125.90.88.*$cip = '218.242.124.'.mt_rand(0,254);$xip = '218.242.124.'.mt_rand(0,254);$header = array( 'CLIENT-IP:'.$cip, 'X-FORWARDED-FOR:'.$xip, );function getimgs( $url,$data,$userinfo,$header){ $ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, "$url"); curl_setopt ($ch, CURLOPT_HTTPHEADER, $header); curl_setopt ($ch, CURLOPT_REFERER, "www.sgs.gov.cn/lz/etpsInfo.do?method=index"); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt ($ch, CURLOPT_USERAGENT, "$userinfo"); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $contents = curl_exec($ch); curl_close($ch); return $contents;}$url ='......余下全文>>
嘗試偽造 頭信息 :Host 、Referer、User-Agent 等