获取PHP抓取远程网站 数据 的 代码

网站数据抓取  时间:2021-02-10  阅读:()

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

Ⅰ .抓取远程网页标题的例子:

以下是代码片段:

?php

/*

+---

+抓取网页标题的代码,直接拷贝本代码片段,另存为.php文件执行即可.+---

*/error_reporting(7) ;

$file=fopen(""r") ;if( !$file) {echo"fontcolor=redUnabletoopenremotefile./font\n";exit;

}while(!feof($file) ) {

$line=fgets($file, 1024) ;if(eregi ("title(.*)/title", $line, $out) ) {

$title=$out[1] ;echo"". $title. "";break;

}

}fclose($file) ;

//End

?

Ⅱ.抓取远程网页HTML代码的例子:

以下是代码片段:

?php

/*

+--

+DNSingSprider

+--

*/

$fp=fsockopen("www.dnsing.com",80, $errno, $errstr,30) ;if(!$fp) {echo"$errstr($errno)br/\n";

}else{

$out="GET/HTTP/1. 1\r\n";

$out.="Host:www.dnsing.com\r\n";

$out.="Connection:Close\r\n\r\n";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_file, $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";echo"Get". $url. ". ";

$url_str=get_content_url (get_url ($url) ) ;echo"OK\n";fwrite($fp, $url_str) ;

++$i ;

}fclose($fp) ;

}

//获取目标多媒体对象functionget_object($url_file, $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_file. "failed") ;foreach($url_arras$url) {if(empty($url) )continue;echo"Get". $url. ". ";

$html_str=get_url ($url) ;echo$html_str;echo$url;exit;

$obj_str=get_content_object($html_str) ;echo"OK\n";fwrite($fp, $obj_str) ;

}fclose($fp) ;

}

//遍历目录获取文件内容

functionget_dir($save_file, $dir) {

$dp=opendir($dir) ;if(file_exists($save_file) )@unlink($save_file) ;

$fp=fopen($save_file, "a+")ordie("Opensavefile". $save_file. "failed") ;while( ($file=readdir($dp) ) !=false) {if($file!=". "&&$file!=". ") {echo"Readfile". $file. ". ";

$file_content=file_get_contents($dir. $file) ;

$obj_str=get_content_object($file_content) ;echo"OK\n";fwrite($fp, $obj_str) ;

}

}fclose($fp) ;

}

//获取指定url内容functionget_url ($url) {

$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($host,80)ordie("Open". $url. "failed") ;

$header="GET/". $url. "HTTP/1. 1\r\n";

$header.="Accept:*/*\r\n";

$header.="Accept-Language:zh-cn\r\n";

$header.="Accept-Encoding:gzip,deflate\r\n";

$header.="User-

Agent:Mozilla/4.0(compatible;MSIE6.0;WindowsNT5. 1 ;SV1 ;Maxthon;InfoPath. 1 ; .NETCLR2.0.50727)\r\n";

$header.="Host:". $host. "\r\n";

$header.="Connection:Keep-Alive\r\n";

//$header.="Cookie:cnzz02=2;rtime=1 ; ltime=1148456424859;cnzz_eid=56601755-\r\n\r\n";

$header.="Connection:Close\r\n\r\n";fwrite($fp, $header) ;while(!feof($fp) ) {

$contents.=fgets($fp,8192) ;

}fclose($fp) ;return$contents;

}

//获取指定内容里的urlfunctionget_content_url ($host_url, $file_contents) {

//$reg='/^ (#|javascript.*?|ftp:\/\/.+|http:\/\/.+| .*?href.*?|play.*?| index.*?| .*?asp)+$/i' ;

//$reg='/^ (down.*?\.html |\d+_\d+\.htm.*?)$/i' ;

$rex="/( [hH] [rR] [eE] [Ff])\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="/href\s*=\s*[' \"]*( [^' \"\s]+) [\"' ]*\s*(b.*?\/b)/i";preg_match_all ($regx, $str, $result) ;if(count($result)==3) {

$result[2]=str_replace("b多媒体:", "", $result[2]) ;

$result[2]=str_replace("/b", "", $result[2]) ;

$result=$result[1] [0] . $split. $result[2] [0] . "\n";

}return$result;

华为云年中聚惠618活动,新用户专区,云服务器低至88元/年,3年仅580.98元,热销抢购中,最后2天!

华为云怎么样?华为云用在线的方式将华为30多年在ICT基础设施领域的技术积累和产品解决方案开放给客户,致力于提供稳定可靠、安全可信、可持续创新的云服务,做智能世界的“黑土地”,推进实现“用得起、用得好、用得放心”的普惠AI。华为云作为底座,为华为全栈全场景AI战略提供强大的算力平台和更易用的开发平台。本次年终聚惠618活动相当给力,1核2G内存1m云耀云服务器仅88元/年起,送主机安全基础版套餐,...

ParkInHost - 俄罗斯VPS主机 抗投诉 55折,月付2.75欧元起

ParkInHost主机商是首次介绍到的主机商,这个商家是2013年的印度主机商,隶属于印度DiggDigital公司,主营业务有俄罗斯、荷兰、德国等机房的抗投诉虚拟主机、VPS主机和独立服务器。也看到商家的数据中心还有中国香港和美国、法国等,不过香港机房肯定不是直连的。根据曾经对于抗投诉外贸主机的了解,虽然ParkInHost以无视DMCA的抗投诉VPS和抗投诉服务器,但是,我们还是要做好数据备...

星梦云:四川100G高防4H4G10M月付仅60元

星梦云怎么样?星梦云资质齐全,IDC/ISP均有,从星梦云这边租的服务器均可以备案,属于一手资源,高防机柜、大带宽、高防IP业务,一手整C IP段,四川电信,星梦云专注四川高防服务器,成都服务器,雅安服务器。星梦云目前夏日云服务器促销,四川100G高防4H4G10M月付仅60元;西南高防月付特价活动,续费同价,买到就是赚到!点击进入:星梦云官方网站地址1、成都电信年中活动机(成都电信优化线路,封锁...

网站数据抓取为你推荐
961556225317563152822是哪个银行的bbsxp怎么用 CUTEFTP上传BBSXP到FTP服务器硬盘人克隆一个人需要多少人多长时间啊开机滚动条怎么减少开机滚动条?畅想中国畅想中国发展前景保护气球什么气球可以骑?qq空间打扮QQ空间打扮安装迅雷看看播放器迅雷看看播放器下了安装不了安全漏洞什么是安全漏洞攻击??小米手柄小米蓝牙游戏手柄怎么连接游戏
深圳主机租用 t楼 cloudstack 宕机监控 mobaxterm godaddy域名转出 realvnc 免费ftp站点 谁的qq空间最好看 重庆双线服务器托管 web服务器是什么 smtp虚拟服务器 服务器维护 国外的代理服务器 lamp什么意思 114dns 云服务是什么意思 .htaccess qq空间打开很慢 卡巴斯基官方下载 更多