要进行网站的自动更新,我们首先要设置我们要采集的页面,假如你要让你的网站自动采集我博客主页上面的所有链接,采集后你可以通过这些链接来采集在主页上标示的文章,那你该怎么做呢?
我写了一个方法来实现这个采集的功能,为了好表述我把采集到的信息放在了files文件下的四个txt文件中,它们是href.txt ,content.txt,link.txt和title.txt
其中content.txt 存放我博客主页的源文件的内容,这个是程序自动写入的,它是我们程序分离出所有链接的数据来源。
href.txt存放说有的链接,形式如<a href=”http://www.baoway.net.cn” >流淌幽居</a>.
link.txt存放从href.txt中分离出来的所有地址,如http://www.baoway.net.cn
title.txt存放从href.txt中分离出来的所有链接名称,如流淌幽居。
我在实现的时候是把该过程写成一个方法的,代码如下:
<?php
/*
$dest为我们要采集链接的页面url
$dns是网站的域名,主要是为了放在采集到的链接的地址不是绝对的url,不是时我们可以通过
链接$dns来获得绝对的URL地址
*/
function getlinks($dest,$dns)//获得链接地址
{
$filesource=$dest;//为网络地址
$filedest=”files/content.txt”;//存放源文件
$filedesta=”files/href.txt”;//存放链接地址与链接标题
$filedestb=”files/title.txt”;//存放链接标题
$filedestc=”files/link.txt”;//存放链接地址
$amount=0;//合法链接的条数
/*把网络中的一个文件打开,并把其中的源代码放入content.txt文件中*/
$text=”";
$fpdest=fopen($filedest,”w+”);
$fpsource=fopen($filesource,”r”);
if($fpsource===false)
return ”;
while(!(feof($fpsource)))
{
$text1=fgetc($fpsource);
$i=ord($text1);
if($i==10||$i==9||$i==11||$i==0||$i==13)
continue;
$text1=strtolower($text1);//转换为小写
$text.=$text1;
}
fwrite($fpdest,$text);//text为源文件
fclose($fpsource);
fclose($fpdest);
/*从content.txt中分离链接,并把内容写入href.txt中*/
$fpsource2=fopen($filedest,”r”) or die(“不能够打开源文件”);
$link=”";
$temp=”";
$isok=false;
$fpdest2=fopen($filedesta,”w+”);
while(!(feof($fpsource2)))
{
$buf=fgetc($fpsource2);
if($buf==”<”)
{
$temp=fgetc($fpsource2);
if($temp==”/”&&($temp=fgetc($fpsource2))==”a”&&($temp=fgetc($fpsource2))==”>”)
{
$link.=”</a>”;
$link.=”\n”;
$isok=false;
fwrite($fpdest2,$link);
$link=”";
}
if($temp==”a”)
{
$link.=$buf;
$buf=$temp;
$isok=true;
}
}
if($isok)
{
$link.=$buf;
}
}
fclose($fpsource2);
fclose($fpdest2);
/*从href.txt中分离出标题,并把标题写入title.txt中*/
$link=”";
$temp=”";
$title=”";
$from=”>”;
$fpsource3=fopen($filedesta,”r”) or die(“不能够打开源文件”);
$fpdest3=fopen($filedestb,”w+”);
while(!(feof($fpsource3)))
{
$link=fgets($fpsource3);
if(substr_count($link,”src=”)==1)//防止链接里有图片等类型
continue;
$temp=strstr($link,$from);
$temp_title=substr($temp,1); //获得<a >后面的字符串如:temp=111<a>
$title=substr($temp_title,0,-5);//获得链接文字
$title.=”\n”;
fwrite($fpdest3,$title);
$link=”";
$temp=”";
$title=”";
}
fclose($fpsource3);
fclose($fpdest3);
/*获得链接地址函数,因为<a href=”" >形式的,因此在href=”和”之间分离出地址*/
function getLinkAddress($s, $from,$to)
{
$fromp=0;
$len=0;
$temp=”";
$link=”";
if(substr_count($s,$from)==1&&substr_count($s,$to)>=1)//防止s里没有$from和$to
{
$fromp=strpos($s,$from);
$len=strlen($from);
$len+=$fromp;
$temp=substr($s,$len);
$link = explode($to, $temp);
$linkaddress=$link[0];
return $linkaddress;
}
return null;
}
/*从href.txt文件中分离出链接的地址,并把它写入到link.txt*/
$fpsource4=fopen($filedesta,”r”) or die(“不能够打开源文件”);
$fpdest4=fopen($filedestc,”w+”);
$link=”";
$linkaddress=”";
$from=”href=\”";
$to=”\”";
while(!(feof($fpsource4)))
{
$link=fgets($fpsource4);
if(substr_count($link,”src=”)==1)//防止链接里有图片等类型
continue;
$amount++;//记录合法链接的条数
$linkaddress=getLinkAddress($link,$from,$to);
$linkaddress.=”\n”;
fwrite($fpdest4,$linkaddress);
$link=”";
$linkaddress=”";
}
fclose($fpsource4);
fclose($fpdest4);
//
/*以下清除重复的链接*/
//echo $amount;
$title=”";
$linkaddress=”";
$links=array();//第一个link数组为空”"
$fpsource5=fopen($filedestb,”r”) or die(“不能够打开源文件”);//取链接的文字
$fpsource6=fopen($filedestc,”r”) or die(“不能够打开源文件”);//取链接的地址
while(!(feof($fpsource5))&&!(feof($fpsource6))&&($amount>0))
{
$title=fgets($fpsource5);
$linkaddress=fgets($fpsource6);
if(substr_count($linkaddress,”http://”)==0)
{
$linkaddress=$dns.$linkaddress;
}
$link_temp=$title.”@”.$linkaddress;
$link=array_push($links,$link_temp);
$amount–;
}
$links=array_unique($links);
fclose($fpsource5);
fclose($fpsource6);
return $links;
}
?>
流淌幽居@http://www.baoway.net.cn
读者可以把它改成一个程序运行,去掉函数的声明和
if($fpsource===false)
return ”;
这段代码,同时显示设置$dest和$dns为http://www.baoway.net.cn看看结果,我执行的结果如下图(部分)
href.txt

link.txt

title.txt

好了介绍到这儿,希望对广大的读者有所帮助!
相关文章:
Comments
Leave a comment Trackback