php抓取远程网站 数据 的 代码

上传人:小** 文档编号:89126556 上传时间:2019-05-19 格式:DOC 页数:7 大小:21.50KB
返回 下载 相关 举报
php抓取远程网站 数据 的 代码_第1页
第1页 / 共7页
php抓取远程网站 数据 的 代码_第2页
第2页 / 共7页
php抓取远程网站 数据 的 代码_第3页
第3页 / 共7页
php抓取远程网站 数据 的 代码_第4页
第4页 / 共7页
php抓取远程网站 数据 的 代码_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《php抓取远程网站 数据 的 代码》由会员分享,可在线阅读,更多相关《php抓取远程网站 数据 的 代码(7页珍藏版)》请在金锄头文库上搜索。

1、PHP抓取远程网站数据的代码PHP抓取远程网站数据的代码2010-12-2815:39现在可能还有很多程序爱好者都会遇到同样的疑问,就是要如何像搜索引擎那样去抓取别人网站的HTML代码,然后把代码收集整理成为自己有用的数据!今天就等我介绍一些简单例子吧.抓取远程网页标题的例子:以下是代码片段:?php/*+-+抓取网页标题的代码,直接拷贝本代码片段,另存为.php文件执行即可.+-*/error_reporting(7);$file=fopen(r);if(!$file)echofontcolor=redUnabletoopenremotefile./fontn;exit;while(!feo

2、f($file)$line=fgets($file,1024);if(eregi(title(.*)/title,$line,$out)$title=$out1;echo.$title.;break;fclose($file);/End?.抓取远程网页HTML代码的例子:以下是代码片段:?php/*+-+DNSingSprider+-*/$fp=fsockopen(,80,$errno,$errstr,30);if(!$fp)echo$errstr($errno)br/n;else$out=GET/HTTP/1.1rn;$out.=Host:rn;$out.=Connection:Closer

3、nrn;fputs($fp,$out);while(!feof($fp)echofgets($fp,128);fclose($fp);/End?以上两个代码片段都直接Copy回去运行就知道效果了,上面的例子只是抓取网页数据的雏形,要使其更适合自己的使用,情况有各异.所以,在此各位程序爱好者自己好好研究一下吧.=稍微有点意义的函数是:get_content_by_socket(),get_url(),get_content_url(),get_content_object几个函数,也许能够给你点什么想法。?php/获取所有内容url保存到文件functionget_index($save_fil

4、e,$prefix=index_)$count=68;$i=1;if(file_exists($save_file)unlink($save_file);$fp=fopen($save_file,a+)ordie(Open.$save_file.failed);while($i$count)$url=$prefix.$i.htm;echoGet.$url.;$url_str=get_content_url(get_url($url);echoOKn;fwrite($fp,$url_str);+$i;fclose($fp);/获取目标多媒体对象functionget_object($url_fi

5、le,$save_file,$split=|-:*:-|)if(!file_exists($url_file)die($url_file.notexist);$file_arr=file($url_file);if(!is_array($file_arr)|empty($file_arr)die($url_file.notcontent);$url_arr=array_unique($file_arr);if(file_exists($save_file)unlink($save_file);$fp=fopen($save_file,a+)ordie(Opensavefile.$save_fi

6、le.failed);foreach($url_arras$url)if(empty($url)continue;echoGet.$url.;$html_str=get_url($url);echo$html_str;echo$url;exit;$obj_str=get_content_object($html_str);echoOKn;fwrite($fp,$obj_str);fclose($fp);/遍历目录获取文件内容functionget_dir($save_file,$dir)$dp=opendir($dir);if(file_exists($save_file)unlink($sa

7、ve_file);$fp=fopen($save_file,a+)ordie(Opensavefile.$save_file.failed);while($file=readdir($dp)!=false)if($file!=.&$file!=.)echoReadfile.$file.;$file_content=file_get_contents($dir.$file);$obj_str=get_content_object($file_content);echoOKn;fwrite($fp,$obj_str);fclose($fp);/获取指定url内容functionget_url($u

8、rl)$reg=/http:/.+$/;if(!preg_match($reg,$url)die($url.invalid);$fp=fopen($url,r)ordie(Openurl:.$url.failed.);while($fc=fread($fp,8192)$content.=$fc;fclose($fp);if(empty($content)die(Geturl:.$url.contentfailed.);return$content;/使用socket获取指定网页functionget_content_by_socket($url,$host)$fp=fsockopen($hos

9、t,80)ordie(Open.$url.failed);$header=GET/.$url.HTTP/1.1rn;$header.=Accept:*/*rn;$header.=Accept-Language:zh-cnrn;$header.=Accept-Encoding:gzip,deflatern;$header.=User-Agent:Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1;Maxthon;InfoPath.1;.NETCLR2.0.50727)rn;$header.=Host:.$host.rn;$header.=Connect

10、ion:Keep-Alivern;/$header.=Cookie:cnzz02=2;rtime=1;ltime=1148456424859;cnzz_eid=56601755-rnrn;$header.=Connection:Closernrn;fwrite($fp,$header);while(!feof($fp)$contents.=fgets($fp,8192);fclose($fp);return$contents;/获取指定内容里的urlfunctionget_content_url($host_url,$file_contents)/$reg=/(#|javascript.*?|

11、ftp:/.+|http:/.+|.*?href.*?|play.*?|index.*?|.*?asp)+$/i;/$reg=/(down.*?.html|d+_d+.htm.*?)$/i;$rex=/(hHrReEFf)s*=s*(s+)*s*/i;$reg=/(down.*?.html)$/i;preg_match_all($rex,$file_contents,$r);$result=;/array();foreach($ras$c)if(is_array($c)foreach($cas$d)if(preg_match($reg,$d)$result.=$host_url.$d.n;return$result;/获取指定内容中的多媒体文件functionget_content_object($str,$split=|-:*:-|)$regx=/hrefs*=s*(s+)*s*(b.*?/b)/i;preg_match_all($regx,$str,$result);if(count($result)=3)$result2=str_replace(b多媒体:,$result2);$result2=str_replace(/b,$result2);$result=$result10.$split.$result20.n;return$result;?

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号