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;
美国服务器哪家平台好?美国服务器无需备案,即开即用,上线快。美国服务器多数带防御,且有时候项目运营的时候,防御能力是用户考虑的重点,特别是网站容易受到攻击的行业。现在有那么多美国一年服务器,哪家的美国云服务器好呢?美国服务器用哪家好?这里推荐易探云,有美国BGP、美国CN2、美国高防、美国GIA等云服务器,线路优化的不错。易探云刚好就是做香港及美国云服务器的主要商家之一,我们来看一下易探云美国云服...
Digital-VM商家的暑期活动促销,这个商家提供有多个数据中心独立服务器、VPS主机产品。最低配置月付80美元,支持带宽、流量和IP的自定义配置。Digital-VM,是2019年新成立的商家,主要从事日本东京、新加坡、美国洛杉矶、荷兰阿姆斯特丹、西班牙马德里、挪威奥斯陆、丹麦哥本哈根数据中心的KVM架构VPS产品销售,分为大硬盘型(1Gbps带宽端口、分配较大的硬盘)和大带宽型(10Gbps...
RAKsmart 商家估摸着前段时间服务器囤货较多,这两个月的促销活动好像有点针对独立服务器。前面才整理到七月份的服务器活动在有一些配置上比上个月折扣力度是大很多,而且今天看到再来部分的服务器首月半价,一般这样的促销有可能是商家库存充裕。比如近期有一些服务商挖矿服务器销售不好,也都会采用这些策略,就好比电脑硬件最近也有下降。不管如何,我们选择服务器或者VPS主机要本着符合自己需求,如果业务不需要,...