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

易探云香港云服务器价格多少钱1个月/1年?

易探云怎么样?易探云是目前国内少数优质的香港云服务器服务商家,目前推出多个香港机房的香港云服务器,有新界、九龙、沙田、葵湾等机房,还提供CN2、BGP及CN2三网直连香港云服务器。近年来,许多企业外贸出海会选择香港云服务器来部署自己的外贸网站,使得越来越多的用户会选择易探云作为网站服务提供平台。今天,云服务器网(yuntue.com)小编来谈谈易探云和易探云服务器怎么样?具体香港云服务器多少钱1个...

BuyVM新设立的迈阿密机房速度怎么样?简单的测评速度性能

BuyVM商家算是一家比较老牌的海外主机商,公司设立在加拿大,曾经是低价便宜VPS主机的代表,目前为止有提供纽约、拉斯维加斯、卢森堡机房,以及新增加的美国迈阿密机房。如果我们有需要选择BuyVM商家的机器需要注意的是注册信息的时候一定要规范,否则很容易出现欺诈订单,甚至你开通后都有可能被禁止账户,也是这个原因,曾经被很多人吐槽的。这里我们简单的对于BuyVM商家新增加的迈阿密机房进行简单的测评。如...

3C云1核1G 9.9元 4核4G 16元 美国Cera 2核4G 24元

3C云互联怎么样?3C云互联专注免备案香港美国日本韩国台湾云主机vps服务器,美国高防CN2GIA,香港CN2GIA,顶级线路优化,高端品质售后无忧!致力于对互联网云计算科技深入研发与运营的极客共同搭建而成,将云计算与网络核心技术转化为最稳定,安全,高速以及极具性价比的云服务器等产品提供给用户!专注为个人开发者用户,中小型,大型企业用户提供一站式核心网络云端服务部署,促使用户云端部署化简为零,轻松...

网站数据抓取为你推荐
iphone360手机卫士苹果手机支持360手机卫士吗?网络明星网络明星是什么,出现这一现象的原因是什么google竞价排名谷歌竞价排名现在是显示在什么位置?绵阳电信绵阳电信宽带资费暴风影音怎么截图请问如何在暴风影音上截图百度手写百度手写显示开机滚动条电脑开机启动滚动条时间长怎么办?如何快速收录谁知道怎么快速被搜索引擎快速收录啊?服务器连接异常手机服务器连接异常php购物车php session实现购物车的原理
如何查询域名备案号 3322动态域名 dns是什么 256m内存 日志分析软件 evssl证书 本网站在美国维护 100x100头像 帽子云 最好的免费空间 100m独享 百度云1t 网通服务器托管 33456 爱奇艺会员免费试用 华为云盘 vul 石家庄服务器托管 中国联通宽带测试 大化网 更多