PHP-文章简单采集

以下是在wamp环境下PHP利用文件操作,获取url,达到文章采集效果

<?php
    //文章采集方法一
/*  $res = fopen("http://www.huanqiu.com/","r");
    $data ='';
    while($strcon = fgets($res)){
        $data .= $strcon;
    }
    fclose($res);*/

    //文章采集方法二
    $data = file_get_contents("http://www.huanqiu.com/");
    $data1 = file_get_contents("http://china.huanqiu.com/article/2017-02/10136020.html");
    $data2 = file_get_contents("http://opinion.huanqiu.com/editorial/2017-02/10135124.html");
    $data3 = file_get_contents("http://world.huanqiu.com/exclusive/2017-02/10137395.html");
    //标题正则
    $div_preg = '/<div id="block_id_42876" class="admin_block" blockid="42876">[\s\S]+?<\/div>/';
    $title_preg = '/<h4>[\s\S]+?<\/h4>/';
    //内容正则
    $con_preg = '/<div class="conText">[\s\S]+?<!-- 左侧 end -->/';
    //得到返回的数组数据
    preg_match($div_preg,$data,$arr);
    preg_match($con_preg,$data1,$arr1);
    preg_match($con_preg,$data2,$arr2);
    preg_match($con_preg,$data3,$arr3);
    //匹配数据里所有需要的标签内容
    preg_match_all($title_preg,$arr[0],$arrT);
?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文章采集</title>
    <style type="text/css">
    ul,li{
        margin: 0;
        padding: 0;
        list-style:none;
    }
    .con{
        width: 420px;
        height: 460px;
        float: left;
        margin-right: 20px;
        overflow: auto;
        border: 2px solid #ccc;
        border-radius: 10px;
        box-shadow: 4px 5px 3px #aaa;
    }
    </style>
</head>
<body>
    <ul>
    <?php foreach ($arrT[0] as $key => $value):?>
        <li><?php echo $value;?></li>
    <?php endforeach;?>
    </ul>
    <ul>
        <div class='con'><?php echo $arr1[0];?></div>
        <div class='con'><?php echo $arr2[0];?></div>
        <div class='con'><?php echo $arr3[0];?></div>
    </ul>
</body>
</html>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值