其实抓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);