php同时提取多条新闻中的文本一例
时间:2007-02-17 来源:PHP爱好者
本文为一个提取一批新闻网页中的文本的小程序,它可以将各篇新闻的内容存为以该新闻标题为文件名的文本文件。如有更好的处理方法,请和我联系:
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
[email protected]
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
这里以人民网中的“今日要闻”下的新闻为例.
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
<?php
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
($url) ? "" : $url = "http://www.unn.com.cn/GB/channel2/3/11/index.html"; // 今日要闻
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
if(isset($url)&&$url!="") {
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str = implode("",file($url));
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str_ary = explode("<ul>",$str);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str_ary = explode("<li>",trim($str_ary[1]));
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
for ($i=0; $i<8; $i++) {
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
if (strlen(trim($str_ary[$i]))<3){
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
continue;
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
}
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
echo "新闻".$i.":".$str_ary[$i];
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str1=strstr("$str_ary[$i]",'<a href="/');
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str2=strstr("$str_ary[$i]",'" target');
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$len1=strlen("$str1");
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$len2=strlen("$str2");
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$len=$len1-$len2;
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$url=substr("$str1",10,$len-10);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
if (strlen(trim($url))!=0) {
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$url = "http://www.unn.com.cn/".$url;
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
define(CONTENTS_DIR,"./contents/");
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
if(isset($url)&&$url!="") {
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str = implode("",file($url));
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str1=explode('<div align="right">',$str); //去掉文件没用的上半部分
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str2 = explode('<h4 align="center"> </h4>',$str1[1]);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
//取出文件的下半部分,并去掉没用的下半部分,这时得到的都是有用的
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str3=explode('</font><font size="+2"><b><font size="3">',$str2[0]); //从整个有用部分取出文件标题和正文
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str4=explode('</div>',$str2[0]); //取出日期和时间
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str5=explode('</font></b></font><font size="2">',$str3[1]); //从标题和正文部分取出标题
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$title=str_replace("<br>","",$str5[0]);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str3=explode('<p><font size="2">',$str2[0]); //从整个有用部分取出文件正文
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str3[1]=str_replace('<br><br> ',"n"." ",$str3[1]);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str3[1]=str_replace(' ',"",$str3[1]);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str3=strip_tags($str3[1]);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$pf=trim($title).".txt";
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$ppf=fopen(CONTENTS_DIR."$pf",'w');
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
fputs($ppf,$title);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
fputs($ppf,"$str4[0]");
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
fputs($ppf,$str3);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
}
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
}
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
}
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
}
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
?>
php爱好者站 http://www.phpfans.net PHP|MySQL|javascript|ajax|html.
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
[email protected]
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
这里以人民网中的“今日要闻”下的新闻为例.
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
<?php
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
($url) ? "" : $url = "http://www.unn.com.cn/GB/channel2/3/11/index.html"; // 今日要闻
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
if(isset($url)&&$url!="") {
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str = implode("",file($url));
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str_ary = explode("<ul>",$str);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str_ary = explode("<li>",trim($str_ary[1]));
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
for ($i=0; $i<8; $i++) {
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
if (strlen(trim($str_ary[$i]))<3){
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
continue;
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
}
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
echo "新闻".$i.":".$str_ary[$i];
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str1=strstr("$str_ary[$i]",'<a href="/');
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str2=strstr("$str_ary[$i]",'" target');
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$len1=strlen("$str1");
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$len2=strlen("$str2");
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$len=$len1-$len2;
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$url=substr("$str1",10,$len-10);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
if (strlen(trim($url))!=0) {
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$url = "http://www.unn.com.cn/".$url;
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
define(CONTENTS_DIR,"./contents/");
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
if(isset($url)&&$url!="") {
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str = implode("",file($url));
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str1=explode('<div align="right">',$str); //去掉文件没用的上半部分
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str2 = explode('<h4 align="center"> </h4>',$str1[1]);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
//取出文件的下半部分,并去掉没用的下半部分,这时得到的都是有用的
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str3=explode('</font><font size="+2"><b><font size="3">',$str2[0]); //从整个有用部分取出文件标题和正文
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str4=explode('</div>',$str2[0]); //取出日期和时间
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str5=explode('</font></b></font><font size="2">',$str3[1]); //从标题和正文部分取出标题
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$title=str_replace("<br>","",$str5[0]);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str3=explode('<p><font size="2">',$str2[0]); //从整个有用部分取出文件正文
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str3[1]=str_replace('<br><br> ',"n"." ",$str3[1]);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str3[1]=str_replace(' ',"",$str3[1]);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$str3=strip_tags($str3[1]);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$pf=trim($title).".txt";
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
$ppf=fopen(CONTENTS_DIR."$pf",'w');
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
fputs($ppf,$title);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
fputs($ppf,"$str4[0]");
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
fputs($ppf,$str3);
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
}
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
}
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
}
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
}
chinaitpower.com3QVTuos2OxyXujw2QGAw935YP
?>
php爱好者站 http://www.phpfans.net PHP|MySQL|javascript|ajax|html.
相关阅读 更多 +