php开发通用采集程序(一)
php采集程序构建基本步骤:
采集程序是什么?获取远程数据(文字、图片、图片)并快速保存到本地或指定地址。
如天气预报(小偷程序):
远程获取--》替换内容--》展示给用户
如实时更新的新闻(采集内容):
远程获取--》提取内容--》分类存储--》读取内容---》展示内容
*************************
设计PHP采集入库UML
列表正则:
终端正则:
**************************
file_get_contents()获取远程页面内容
preg_match_all()匹配列表
preg_match()匹配终端
preg_replace() :过滤终端
**************************
新建一个列表文件:lb.php
echo $con=file_get_cotents($_GET[url]);
$preg ="#
(.*)#iUs";
preg_match_all($preg,$conn,$arr);
foreach($arr[1] as $id=>$v){
echo $v." "arr[2][$id]."
";
}
新建vi。php获取标题
vi.php
$con=file_get_contents();
$preg="
(.*)/iUs";//大小写不敏感,防止贪婪匹配,防止换行echo zz($preg,$con);
function zz($preg,$con,$num=1){
preg_match($preg,$con,$arr);
return $arr[$num];
}
echo $arr[1];//获取标题
echo "".arr[2][$id]."
";
======================================
php100:83:采集程序
lb.php
if($_GET[id]){
$con=file_get_cotents($_GET[url].$_GET[id]."shtml");
$preg ="/
(.*)/iUs";
perg_match_all($preg,$conn,$arr);
foreach($arr[1] as $id=>$v){
echo $v." "arr[2][$id]."
";
}
$_GET[id]++;
echo "";
}
?>
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
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="#
(.*)#iUs";preg_match_all($preg,$con,$arr);
foreach ($arr[2] as $id=>$v){
echo "".$arr[3][$id]."
";
$sql="INSERT INTO `caiji` (`id`, `title`, `url`) VALUES (NULL, '".$arr[3][$id]."', '".$v."')";
mysql_query($sql);
}
@$_GET['id']++;
echo "正在采集列表....".@$_GET['id'];
echo "";
}else{
echo "采集结束";
}
?>
vi.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("#
(.*)#iUs",$con);echo $con=zz("#
- (.*)
$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 "";
}
function zz($preg,$con,$num=1){
preg_match($preg,$con,$arr);
return $arr[$num];
}
?>