PHP版,文章采集URL补全函数:formaturl
[color=Red]Lukin原创作品,欢迎转载,请保留本页链接。[/color][color=Green]文章原文:[url]http://hi.baidu.com/lukin/blog/item/a82df3039c21fded09fa93c9.html[/url][/color]
写采集必用的函数,URL补全函数,也可叫做FormatUrl。
写此函数作用就是为了开发采集程序,采集文章的时候会经常遇到页面里的路径是 “相对路径” 或者 “绝对根路径” 不是“绝对全路径”就无法收集URL。
所以,就需要本功能函数进行对代码进行格式化,把所有的超链接都格式化一遍,这样就可以直接收集到正确的URL了。
路径知识普及
相对路径:“../” “./” 或者前面什么都不加
绝对根路径:/path/xxx.html
绝对全路径:[url]http://www.xxx.com/path/xxx.html[/url]
使用实例:
[php]
<?php
$surl="http://www.7lian.com/";
$gethtm = '<a href="/index.htm">首页</a><a href="Resolvent/index.htm">解决方案</a>';
echo formaturl($gethtm,$surl);
?>
[/php]
输出:<a href="http://www.7lian.com/index.htm">首页</a><a href="http://www.7lian.com/Resolvent/index.htm">解决方案</a>
--------- 演示实例 ------------
原始路径代码:[url]http://www.newnew.cn/newnewindex.aspx[/url]
输出演示代码:[url]http://www.maifp.com/aaa/test.php[/url]
[php]
<?php
function formaturl($l1,$l2){
if (preg_match_all("/(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<a[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src='([^']+)'[^>]*>)|(<a[^>]+href='([^']+)'[^>]*>)/i",$l1,$regs)){
foreach($regs[0] as $num => $url){
$l1 = str_replace($url,lIIIIl($url,$l2),$l1);
}
}
return $l1;
}
function lIIIIl($l1,$l2){
if(preg_match("/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i",$l1,$regs)){$I2 = $regs[3];}
if(strlen($I2)>0){
$I1 = str_replace(chr(34),"",$I2);
$I1 = str_replace(chr(39),"",$I1);
}else{return $l1;}
$url_parsed = parse_url($l2);
$scheme = $url_parsed["scheme"];if($scheme!=""){$scheme = $scheme."://";}
$host = $url_parsed["host"];
$l3 = $scheme.$host;
if(strlen($l3)==0){return $l1;}
$path = dirname($url_parsed["path"]);if($path[0]=="\\"){$path="";}
$pos = strpos($I1,"#");
if($pos>0) $I1 = substr($I1,0,$pos);
//判断类型
if(preg_match("/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&)|&)+/i",$I1)){return $l1; }//http开头的url类型要跳过
elseif($I1[0]=="/"){$I1 = $l3.$I1;}//绝对路径
elseif(substr($I1,0,3)=="../"){//相对路径
while(substr($I1,0,3)=="../"){
$I1 = substr($I1,strlen($I1)-(strlen($I1)-3),strlen($I1)-3);
if(strlen($path)>0){
$path = dirname($path);
}
}
$I1 = $l3.$path."/".$I1;
}
elseif(substr($I1,0,2)=="./"){
$I1 = $l3.$path.substr($I1,strlen($I1)-(strlen($I1)-1),strlen($I1)-1);
}
elseif(strtolower(substr($I1,0,7))=="mailto:"||strtolower(substr($I1,0,11))=="javascript:"){
return $l1;
}else{
$I1 = $l3.$path."/".$I1;
}
return str_replace($I2,"\"$I1\"",$l1);
}
?>
[/php]
[[i] 本帖最后由 Lukin 于 2007-4-23 01:25 AM 编辑 [/i]] 挺复杂的样子
你可以用当前url加上html中url
然后用realpath() 要是按照你说的那么好实现,就不需要写这些了! 连javascript都匹配呀,:),看到很不错的样子,不知断行的链接怎样? javascript不匹配 只匹配html里面的
断行的链接应该不好匹配
不过会有sb 把链接断行吗? 正如楼上所说,sb 不会去做断行的。 程序生成的话,有可能换行,看看网易首页的代码
[code]<div class="rightTitle rightLine">
<h5><a href="http://blog.163.com/">博客</a></h5><span><a
href="http://blog.163.com/redirect.html?url=http://ent.163.com/special/0003sp/starface2007.html
?0704ldq01"><strong class="cDRed"
style="font-weight: normal">自拍斗秀场,有你参加更精彩</strong></a> <a
href="http://blog.163.com/redirect.html?url=http://ent.163.com/special/0003sp/starface2007.html
?0704ldq01
"><strong class="cDRed"
style="font-weight: normal;display:none;">快来挑战网易编辑</strong></a> <a
href="http://vip.twgaoming.blog.163.com/?fromExperience"><strong class="cDRed"
style="font-weight: normal;display:none;">高明</strong></a> <a
href="http://zhanghf029.blog.163.com/?fromExperience"><strong class="cDRed"
style="font-weight: normal;display:none;">张子丰</strong></a> <a
href="http://blog.163.com/vip.huosiyan/?fromExperience"><strong class="cDRed"
style="font-weight: normal;display:none;">霍思燕</strong></a> </span></div>
<div class="blogCommend">[/code] 断行问题好解决啊
你只要把所有的回车全部过滤掉
然后再用这个函数采集就可以了哦~ [quote]原帖由 [i]Lukin[/i] 于 2007-4-28 04:23 PM 发表
javascript不匹配 只匹配html里面的
断行的链接应该不好匹配
不过会有sb 把链接断行吗? [/quote]
原来我是sb啊 if(preg_match("/^(http|https|ftp):(//|\\)(([w/\+-~`@:%])+.)+([w/\.=?+-~`@':!%#]|(&)|&)+/i",$I1)){return $l1; }//
这个正则调试了有错,麻烦楼主看看。 找这个好久了,多谢,学习学习 采集一直想研究
就没时间研究 恩 用采集器很简单的!
页:
[1]