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

CloudCone(12.95美元/月CN2 GT线路,KVM架构1 Gbps带宽

整理一下CloudCone商家之前推送的闪购VPS云服务器产品,数量有限,活动推出可能很快机器就售罄了,有需要美国便宜VPS云服务器的朋友可以关注一下。CloudCone怎么样?CloudCone服务器好不好?CloudCone值不值得购买?CloudCone是一家成立于2017年的美国服务器提供商,国外实力大厂,自己开发的主机系统面板,CloudCone主要销售美国洛杉矶云服务器产品,优势特点是...

RAKsmart:美国圣何塞服务器限量秒杀$30/月起;美国/韩国/日本站群服务器每月189美元起

RAKsmart怎么样?RAKsmart是一家由华人运营的国外主机商,提供的产品包括独立服务器租用和VPS等,可选数据中心包括美国加州圣何塞、洛杉矶、中国香港、韩国、日本、荷兰等国家和地区数据中心(部分自营),支持使用PayPal、支付宝等付款方式,网站可选中文网页,提供中文客服支持。本月商家继续提供每日限量秒杀服务器月付30.62美元起,除了常规服务器外,商家美国/韩国/日本站群服务器、1-10...

HostKvm开年促销:香港国际/美国洛杉矶VPS七折,其他机房八折

HostKvm也发布了开年促销方案,针对香港国际和美国洛杉矶两个机房的VPS主机提供7折优惠码,其他机房业务提供8折优惠码。商家成立于2013年,提供基于KVM架构的VPS主机,可选数据中心包括日本、新加坡、韩国、美国、中国香港等多个地区机房,均为国内直连或优化线路,延迟较低,适合建站或者远程办公等。下面列出几款主机配置信息。美国洛杉矶套餐:美国 US-Plan1CPU:1core内存:2GB硬盘...

网站数据抓取为你推荐
刷网站权重怎么才能提升网站百度权重呢如何免费开通黄钻怎么免费开通黄钻~~~?公章制作在WOLD里怎样制作公章qq空间装扮qq空间的装扮空间在哪?如何建立一个网站如何建立一个网站?蘑菇街美丽说蘑菇街美丽说唯品会天猫京东。女生买衣服,哪个好开机滚动条开机滚动条要很长时间怎么解决?保护气球抖音里面看的,这是什么游戏雅虎天盾高手进来看看我该怎么办 新装的ie8 内存使用率达到100%了xp系统停止服务XP系统停止服务后电脑怎么办?
韩国服务器租用 美国主机评测 安云加速器 海外服务器 特价空间 permitrootlogin 阿里云代金券 好看的桌面背景图片 php免费空间 湖南服务器托管 我爱水煮鱼 怎么测试下载速度 股票老左 服务器监测 免费网页申请 申请网页 万网空间购买 shopex主机 无限流量 dnspod 更多