<?php
set_time_limit(0);
header("content-type:text/html;charset=utf8");
mysql_connect("127.0.0.1","root",'root');
mysql_select_db("seven_month");
mysql_query("set names utf8");
include 'Snoopy.class.php';
$snoopy=new Snoopy;
$url="http://www.duwenzhang.com/";
$snoopy->fetch($url);
$content=$snoopy->results;
$contents = iconv("gb2312","utf-8//IGNORE",$content);
$preg='#<td class="blue sprite-bluec"> <a href="(.*)">(.*)</a></td>#isU';
preg_match_all($preg,$contents,$type);
$type=$type[2];
$preg1='#<TR>(.*)<TD width="417" class="inddline">(.*)<a href="(.*)">(.*)</a></TD>(.*)<TD width="76" align="right" class="inddline">(.*)</TD>(.*)</TR>#isU';
preg_match_all($preg1,$contents,$arr);
$links=$arr[3];
foreach($links as $key=>$val){
$article=file_get_contents($val);
$article=iconv("gb2312","utf-8//IGNORE",$article);
$preg_text='#<div id="wenzhangziti" class="(.*)">(.*)</div>#isU';
preg_match_all($preg_text,$article,$text);
$newtext[]=$text[2][0];
}
$newtext=array_chunk($newtext,5);
$time=array_chunk($arr[6],5);
$title=$arr[4];
$title=array_chunk($title,5);
$new=array();
for($i=0;$i<count($title);$i++) {
$new[$i]['title']=$title[$i];
$new[$i]['add_time'] = $time[$i];
$new[$i]['text']=$newtext[$i];
}
foreach($type as $key=>$val){
$sql="insert into type (type_name) values ('".$val."')";
mysql_query($sql);
$id=mysql_insert_id();
foreach($new as $key=>$val){
$sql="insert into title_type(title,add_time,text,type_id) VALUES ";
for($i=0;$i<count($val['title']);$i++){
$sql.="('".$val['title'][$i]."','".$val['add_time'][$i]."','".$val['text'][$i]."',$id)".',';
mysql_query($sql);
$sql1=substr($sql,0,-1);
$re=mysql_query($sql1);
}
}
}
if(isset($re) && $re=true){
echo "恭喜你采集成功";
}else{
echo "很抱歉,采集失败,请重新采集";
}
$pdo=new PDO("mysql:host=localhost;dbname=seven_month","root","root");
$pdo->exec("set names utf-8");
$data=$pdo->query("select * from type")->FETCHALL(PDO::FETCH_ASSOC);
print_r($data);
set_time_limit(0);
header("content-type:text/html;charset=utf8");
mysql_connect("127.0.0.1","root",'root');
mysql_select_db("seven_month");
mysql_query("set names utf8");
include 'Snoopy.class.php';
$snoopy=new Snoopy;
$url="http://www.duwenzhang.com/";
$snoopy->fetch($url);
$content=$snoopy->results;
$contents = iconv("gb2312","utf-8//IGNORE",$content);
$preg='#<td class="blue sprite-bluec"> <a href="(.*)">(.*)</a></td>#isU';
preg_match_all($preg,$contents,$type);
$type=$type[2];
$preg1='#<TR>(.*)<TD width="417" class="inddline">(.*)<a href="(.*)">(.*)</a></TD>(.*)<TD width="76" align="right" class="inddline">(.*)</TD>(.*)</TR>#isU';
preg_match_all($preg1,$contents,$arr);
$links=$arr[3];
foreach($links as $key=>$val){
$article=file_get_contents($val);
$article=iconv("gb2312","utf-8//IGNORE",$article);
$preg_text='#<div id="wenzhangziti" class="(.*)">(.*)</div>#isU';
preg_match_all($preg_text,$article,$text);
$newtext[]=$text[2][0];
}
$newtext=array_chunk($newtext,5);
$time=array_chunk($arr[6],5);
$title=$arr[4];
$title=array_chunk($title,5);
$new=array();
for($i=0;$i<count($title);$i++) {
$new[$i]['title']=$title[$i];
$new[$i]['add_time'] = $time[$i];
$new[$i]['text']=$newtext[$i];
}
foreach($type as $key=>$val){
$sql="insert into type (type_name) values ('".$val."')";
mysql_query($sql);
$id=mysql_insert_id();
foreach($new as $key=>$val){
$sql="insert into title_type(title,add_time,text,type_id) VALUES ";
for($i=0;$i<count($val['title']);$i++){
$sql.="('".$val['title'][$i]."','".$val['add_time'][$i]."','".$val['text'][$i]."',$id)".',';
mysql_query($sql);
$sql1=substr($sql,0,-1);
$re=mysql_query($sql1);
}
}
}
if(isset($re) && $re=true){
echo "恭喜你采集成功";
}else{
echo "很抱歉,采集失败,请重新采集";
}
$pdo=new PDO("mysql:host=localhost;dbname=seven_month","root","root");
$pdo->exec("set names utf-8");
$data=$pdo->query("select * from type")->FETCHALL(PDO::FETCH_ASSOC);
print_r($data);