分享自动采集新闻内容的php脚本

今天分享一个php脚本,全自动采集新闻内容,以p标签分割内容,每个p标签为一行句子,保存到文件夹txt

脚本代码:

header('Content-type:text/html; charset=utf-8');  
// www.DiDitest  玩站弟弟  
function sina()  
{  
    $html = file_get_contents('http://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2510&k=&num=300&page=1');  
    preg_match_all("/\"url\":\"(.*?)\",/", $html, $urls);  
    foreach ($urls['1'] as $url) {  
        $wz_url = str_replace('https', 'http', $url);  
        $wz_url = str_replace('\\', '', $wz_url);  
        $html = file_get_contents($wz_url);  
        preg_match("/<h1 class=\"main-title\">(.*?)<\/h1>/", $html, $titles);  
        preg_match("/<div class=\"article\" id=\"article\">.*?<p class=\"show_author\">/s", $html, $contents);  
        preg_match_all("/<p>(.*?)<\/p>/", $contents['0'], $juzis);  
        foreach ($juzis['1'] as $juzi)  
        {  
            $wz_juzi = ltrim($juzi, " ");  
            $wz_juzi = preg_replace("/<.*?>/", '', $wz_juzi);  
            if (!strstr($juzi, '原标题')) {  
                if (mb_strlen($wz_juzi, 'UTF-8') > 60) {  
                    file_put_contents(str_replace('\\','/',__DIR__).'/diditest/'. date("Ymd") . '.txt', $juzi . PHP_EOL, FILE_APPEND);  
                }  
            }  
        }  
  
        echo $titles['1'] . " >>> 采集完成" . "<br>";  
    }  
}  
sina();  

这个代码保存后,会在diditest文件夹,生成当天日期为命名的txt,生成采集下来,分割后的句子内容

1.站点支持支付宝扫码支付,更多支付方式联系客服QQ!
2.客服QQ:3013197813822674928
3.本站大部分下载资源收集于网络,仅供个人学习与交流使用,版权归原作者所有!
4.严禁用于非法商业活动,遵守相关法律法规,违者自行承担后果!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
搜索