php抓取页面部分内容
1、<?phpecho "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";//防止中文乱码$url = "http://www.aizhan.com/cha/www.qihong.com/"; //要抓取的页面$contents = file_get_contents($url); //抓取页面所有内容存入字符串?>
2、使用字符串替代,清楚一部分不需要的代码(这里不使用正则直接取需要的代码,是由于部分js或者css的调用)&造婷用痃lt;?php $contents=str_replace("api/js/wb.js", "", $contents);$contents=str_replace("baidu1f", "", $contents);$contents=str_replace("baidu6f", "", $contents);$contents=str_replace("baidu4f", "", $contents);$contents=str_replace("baidu7f", "", $contents);$contents=str_replace("baiduf1f", "", $contents);$contents=str_replace("js/tjs.js", "", $contents);$contents=str_replace("24小时收录", "24时收录", $contents);?>
3、使用css蒙版隐藏不需要的内容<style>.header,.path,.s-hl-content{display:none;}#BAIDU_SSP__wrapper_u2297132_0,#main_title,#tabs_page{display:none;}#eee{border:0px solid red;margin-top:-118px;width:787px;height:450px;overflow:hidden;}</style><div id="eee"><?php echo $contents;?></div>