• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

同时提取多条新闻中的文本一例_PHP

php 搞代码 3年前 (2022-01-25) 13次浏览 已收录 0个评论

本文为一个提取一批新闻网页中的文本的小程序,它可以将各篇新闻的内容存为以该新闻标题为文件名的文本文件。如有更好的处理方法,请和我联系:
[email protected]@本文来源gaodaimacom搞#代%码@网-搞代gaodaima码
这里以人民网中的“今日要闻”下的新闻为例.
<?php
($url) ? “” : $url = “http://www.unn.com.cn/GB/channel2/3/11/index.html&#8221;; // 今日要闻
if(isset($url)&&$url!=””) {
$str = implode(“”,file($url));
$str_ary = explode(“

    “,$str);
    $str_ary = explode(“

  • “,trim($str_ary[1]));
    for ($i=0; $i<8; $i ) {
    if (strlen(trim($str_ary[$i]))<3){
    continue;
    }
    echo “新闻”.$i.”:”.$str_ary[$i];
    $str1=strstr(“$str_ary[$i]”,’ $str2=strstr(“$str_ary[$i]”,'” target’);
    $len1=strlen(“$str1”);
    $len2=strlen(“$str2”);
    $len=$len1-$len2;
    $url=substr(“$str1”,10,$len-10);
    if (strlen(trim($url))!=0) {
    $url = “http://www.unn.com.cn/&#8221;.$url;
    define(CONTENTS_DIR,”./contents/”);
    if(isset($url)&&$url!=””) {
    $str = implode(“”,file($url));
    $str1=explode(‘
    ‘,$str); //去掉文件没用的上半部分
    $str2 = explode(‘

    ‘,$str1[1]);
    //取出文件的下半部分,并去掉没用的下半部分,这时得到的都是有用的
    $str3=explode(‘‘,$str2[0]); //从整个有用部分取出文件标题和正文
    $str4=explode(‘

    ‘,$str2[0]); //取出日期和时间
    $str5=explode(‘
    ‘,$str3[1]); //从标题和正文部分取出标题
    $title=str_replace(“
    “,””,$str5[0]);
    $str3=explode(‘

    ‘,$str2[0]); //从整个有用部分取出文件正文
    $str3[1]=str_replace(‘

    ‘,”\n”.”  ”,$str3[1]);
    $str3[1]=str_replace(‘ ‘,””,$str3[1]);
    $str3=strip_tags($str3[1]);
    $pf=trim($title).”.txt”;
    $ppf=fopen(CONTENTS_DIR.”$pf”,’w’);
    fputs($ppf,$title);
    fputs($ppf,”$str4[0]”);
    fputs($ppf,$str3);
    }
    }
    }
    }
    ?>


    搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
    转载请注明原文链接:同时提取多条新闻中的文本一例_PHP
    喜欢 (0)
    [搞代码]
    分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址