zoukankan      html  css  js  c++  java
  • 采集入库程序原理

    php开发通用采集程序(一)

    php采集程序构建基本步骤:
    采集程序是什么?获取远程数据(文字、图片、图片)并快速保存到本地或指定地址。
    如天气预报(小偷程序):
    远程获取--》替换内容--》展示给用户
    如实时更新的新闻(采集内容):
    远程获取--》提取内容--》分类存储--》读取内容---》展示内容


    *************************
    设计PHP采集入库UML
    列表正则:
    终端正则:
    **************************
    file_get_contents()获取远程页面内容
    preg_match_all()匹配列表
    preg_match()匹配终端
    preg_replace() :过滤终端
    **************************
    新建一个列表文件:lb.php
    echo $con=file_get_cotents($_GET[url]);
    $preg ="#<h1><a href='(.*)' target='_blank'>(.*)</a><span>#iUs";

    preg_match_all($preg,$conn,$arr);
    foreach($arr[1] as $id=>$v){
    echo $v." "arr[2][$id]."<br>";
    }

    新建vi。php获取标题

    vi.php

    $con=file_get_contents();
    $preg="<title>(.*)</title>/iUs";//大小写不敏感,防止贪婪匹配,防止换行
    echo zz($preg,$con);
    function zz($preg,$con,$num=1){
    preg_match($preg,$con,$arr);
    return $arr[$num];
    }


    echo $arr[1];//获取标题
    echo "<a href=vi.php?url=".$v.">".arr[2][$id]."<br>";
    ======================================
    php100:83:采集程序

    lb.php
    <?php
    if($_GET[id]){
    $con=file_get_cotents($_GET[url].$_GET[id]."shtml");
    $preg ="/<h1><a href='(.*)' target='_blank'>(.*)</a><span>/iUs";

    perg_match_all($preg,$conn,$arr);
    foreach($arr[1] as $id=>$v){
    echo $v." "arr[2][$id]."<br>";
    }
    $_GET[id]++;

    echo "<script>location.href='lb.php?id='.$_GET[id]."</script>";
    }
    ?>

    http://www.xinhuanet.com/finance/tzxx.htm

    ===============================================================
    操作步骤:
    1.新建一个数据库caiji,tep_url表,id,title,content,11 ,100,150
    2.新建一个conn.php,插入的语句可以通过phpmyadmin插入一个语句简单的获得
    3.新建一个vi.php
    4.单条入库获取下一条。

    临时的路径列表:
    完善我的入库表。


    假设删删除了其中的一条Id值,怎么办?删除了第9条!
    多加一条语句
    $sql2="select * from tmp_url where id>'$gid'order by id asc limit 1

    code:

    ls.php

    <?php
    include_once('conn.php');
    if(@$_GET['id']<=6 && @$_GET['id']){
      if(@$_GET['id']==1){
    $con=file_get_contents("http://www.php100.com/html/shipinjiaocheng/PHP100shipinjiaocheng/");
    }else{
    $con=file_get_contents("http://www.php100.com/html/shipinjiaocheng/PHP100shipinjiaocheng/".@$_GET['id'].".html");
    }
    $preg="#<td height="25"><img src="(.*)"> <a href="(.*)" style="font-size:14px"><strong><b>(.*)</b></strong></a></td>#iUs";

    preg_match_all($preg,$con,$arr);

    foreach ($arr[2] as $id=>$v){
     echo "<a href=vi.php?url=".$v.">".$arr[3][$id]."</a><br>";
     
     $sql="INSERT INTO `caiji` (`id`, `title`, `url`) VALUES (NULL, '".$arr[3][$id]."', '".$v."')";
     mysql_query($sql);
        }
        @$_GET['id']++;
       
        echo "正在采集列表....".@$_GET['id'];
        echo "<script>location.href='ls.php?id=".@$_GET['id']."'</script>";
    }else{
     echo "采集结束";
    }
    ?>

    vi.php

    <?php

    include_once('conn.php');
    $gid=(int)@$_GET['id'];
    $sql="select * from `caiji` where `id`='$gid'";
    $query=mysql_query($sql);
    $row=mysql_fetch_array($query);

        $con=file_get_contents($row['url']);
        echo $title=zz("#<title>(.*)</title>#iUs",$con);
        echo $con=zz("#<ul class="downurllist">(.*)</ul>#iUs",$con);
        $content=addslashes($con);
        $intosql="INSERT INTO `caiji`.`news` (`id`, `title`, `content`) VALUES (NULL,'$title','$content')";
        mysql_query($intosql);

    $sql2="select * from `caiji` where `id`>'$gid' order by `id` asc limit 1";
    $query2=mysql_query($sql2);
    $row2=mysql_fetch_array($query2);
    if($row2[0]){
    echo "<script>location.href='vi.php?id=".$row2[0]."'</script>";
    }

    function zz($preg,$con,$num=1){
        preg_match($preg,$con,$arr);
        return $arr[$num]; 
        }

    ?>

  • 相关阅读:
    A1023 Have Fun with Numbers (20分)(大整数四则运算)
    A1096 Consecutive Factors (20分)(质数分解)
    A1078 Hashing (25分)(哈希表、平方探测法)
    A1015 Reversible Primes (20分)(素数判断,进制转换)
    A1081 Rational Sum (20分)
    A1088 Rational Arithmetic (20分)
    A1049 Counting Ones (30分)
    A1008 Elevator (20分)
    A1059 Prime Factors (25分)
    A1155 Heap Paths (30分)
  • 原文地址:https://www.cnblogs.com/smartyman/p/3798382.html
Copyright © 2011-2022 走看看