进入开发阶段 – 单条入库获取下一条
实现真正大批量的入库功能

--
-- 数据库: `caiji`
--

-- --------------------------------------------------------

--
-- 表的结构 `news`
--

CREATE TABLE `news` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(50) collate gb2312_bin NOT NULL,
  `content` text collate gb2312_bin NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=gb2312 COLLATE=gb2312_bin AUTO_INCREMENT=58 ;

-- --------------------------------------------------------

--
-- 表的结构 `tmp_url`
--

CREATE TABLE `tmp_url` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(100) NOT NULL,
  `url` varchar(150) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=gb2312 AUTO_INCREMENT=166 ;

 

<?php
 
  mysql_connect("localhost","root","");
  mysql_select_db("caiji");
  mysql_query("set names 'GBK'");

?>

 

 

<?php
 include_once("conn.php");


  if($_GET[id]<=60 && $_GET[id]){
  $con=file_get_contents("http://it.sohu.com/7/1002/17/column203661721_32".$_GET[id].".shtml");

  $preg = "#<h1>&middot;<a href='(.*)' target='_blank'>(.*)</a><span>#iUs";

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

   foreach($arr[1] as $id=>$v){

   $sql="INSERT INTO `tmp_url` (`id`, `title`, `url`) VALUES (NULL, '".$arr[2][$id]."', '".$v."');";
   mysql_query($sql);

     }
  $_GET[id]++;
     echo "正在采集列表....".$_GET[id];
  echo "<script>location.href='ls.php?id=".$_GET[id]."'</script>";
  }else{
 
     echo "采集结束";
  }
?>

 

 

<?php
   include_once("conn.php");

  $gid =(int)$_GET[id];
  
   $sql="select * from tmp_url where id='$gid'";
   $q=mysql_query($sql);
   $row = mysql_fetch_array($q);
  
   $con=file_get_contents($row[url]);
    echo $titles="<b>".zz("#<title>(.*)</title>#iUs",$con)."</b>";

    echo $conts=zz("#<!-- 正文 st -->(.*)<!-- 正文 end -->#iUs",$con);
 
    $intosql="INSERT INTO `news` (`id`, `title`, `content`) VALUES (NULL, '$titles', '$conts');";
 mysql_query($intosql);


 //============================

   $sql2="select * from tmp_url where id>'$gid' order by id asc limit 1";
   $q2=mysql_query($sql2);
   $row2 = mysql_fetch_array($q2);
    echo $row2[0].$row2[1]."<br>";
    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];
  }
?>