获取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;

HostKvm5.95美元起,香港、韩国可选

HostKvm发布了夏季特别促销活动,针对香港国际/韩国机房VPS主机提供7折优惠码,其他机房全场8折,优惠后2GB内存套餐月付仅5.95美元起。这是一家成立于2013年的国外主机服务商,主要提供基于KVM架构的VPS主机,可选数据中心包括日本、新加坡、韩国、美国、中国香港等多个地区机房,均为国内直连或优化线路,延迟较低,适合建站或者远程办公等。下面分享几款香港VPS和韩国VPS的配置和价格信息。...

DogYun27.5元/月香港/韩国/日本/美国云服务器,弹性云主机

DogYun怎么样?DogYun是一家2019年成立的国人主机商,称为狗云,提供VPS及独立服务器租用,其中VPS分为经典云和动态云(支持小时计费及随时可删除),DogYun云服务器基于Kernel-based Virtual Machine(Kvm)硬件的完全虚拟化架构,您可以在弹性云中,随时调整CPU,内存,硬盘,网络,IPv4路线(如果该数据中心接入了多条路线)等。DogYun弹性云服务器优...

Sharktech$129/月,1Gbps不限流量,E5-2678v3(24核48线程)

Sharktech最近洛杉矶和丹佛低价配置大部分都无货了,只有荷兰机房还有少量库存,商家又提供了两款洛杉矶特价独立服务器,价格不错,CPU/内存/硬盘都是高配,1-10Gbps带宽不限流量最低129美元/月起。鲨鱼机房(Sharktech)我们也叫它SK机房,是一家成立于2003年的老牌国外主机商,提供的产品包括独立服务器租用、VPS主机等,自营机房在美国洛杉矶、丹佛、芝加哥和荷兰阿姆斯特丹等,主...

网站数据抓取为你推荐
fontfamily这是什么字体,求解.. font-family: PahuengaCassRegular;优酷路由宝怎么赚钱优酷路由宝整的可以赚钱吗?可以赚多少?中国论坛大全安徽论坛都有哪些?怎么在qq空间里添加背景音乐怎样在qq空间里免费添加背景音乐?镜像文件是什么镜像文件是什么意思?如何建立自己的网站怎么创建自己的网站怎么点亮qq空间图标怎样点亮qq空间的图标数据库损坏数据库坏了,怎么修复?ios系统苹果手机的系统是什么?什么是云平台什么是家庭云平台?
成都虚拟主机 备案域名出售 sugarhosts 网站实时监控 中国电信测速112 bgp双线 秒杀汇 中国电信测速网 卡巴斯基破解版 789电视剧 环聊 英国伦敦 免费asp空间 wordpress中文主题 电信主机托管 免费获得q币 美国主机 comodo 服务器是什么 服务器操作系统 更多