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

ZoeCloud:香港BGP云服务器,1GB内存/20GB SSD空间/2TB流量/500Mbps/KVM,32元/月

zoecloud怎么样?zoecloud是一家国人商家,5月成立,暂时主要提供香港BGP KVM VPS,线路为AS41378,并有首发永久8折优惠:HKBGP20OFF。目前,解锁香港区 Netflix、Youtube Premium ,但不保证一直解锁,谢绝以不是原生 IP 理由退款。不保证中国大陆连接速度,建议移动中转使用,配合广州移动食用效果更佳。点击进入:zoecloud官方网站地址zo...

特网云(198元/月),高质量云虚拟主机低至0.16元/天,裸金属服务器仅需10.5元/天

特网云为您提供高速、稳定、安全、弹性的云计算服务计算、存储、监控、安全,完善的云产品满足您的一切所需,深耕云计算领域10余年;我们拥有前沿的核心技术,始终致力于为政府机构、企业组织和个人开发者提供稳定、安全、可靠、高性价比的云计算产品与服务。官方网站:https://www.56dr.com/ 10年老品牌 值得信赖 有需要的请联系======================特网云推出多IP云主机...

半月湾($59.99/年),升级带宽至200M起步 三网CN2 GIA线路

在前面的文章中就有介绍到半月湾Half Moon Bay Cloud服务商有提供洛杉矶DC5数据中心云服务器,这个堪比我们可能熟悉的某服务商,如果我们有用过的话会发现这个服务商的价格比较贵,而且一直缺货。这里,于是半月湾服务商看到机会来了,于是有新增同机房的CN2 GIA优化线路。在之前的文章中介绍到Half Moon Bay Cloud DC5机房且进行过测评。这次的变化是从原来基础的年付49....

网站数据抓取为你推荐
怎么样免费装扮qq空间如何免费装扮qq空间ps抠图技巧ps中怎么抠图?vbscript教程vbs 学习方法以及 vbs 实例 有编程基础神雕侠侣礼包大全神雕侠侣手游版四重大礼包怎么得到啊?人人逛街人人都喜欢逛街吗bluestackbluestacks下载的东西在哪什么是云平台什么是云系统?rewritebasehttp怎么做自动跳转https声母是什么什么是声母,什么是韵母微信怎么看聊天记录微信如何查找聊天记录
海外虚拟主机 域名网 备案域名购买 域名查询软件 免费动态域名解析 老左 电信测速器 photonvps 外国服务器 ssh帐号 一点优惠网 mysql主机 云鼎网络 免费全能空间 亚洲小于500m gg广告 ftp教程 网站cdn加速 静态空间 中国网通测速 更多