Home  >  Article  >  php教程  >  基于PHP的简单采集数据入库程序,php采集入库

基于PHP的简单采集数据入库程序,php采集入库

WBOY
WBOYOriginal
2016-06-13 09:27:53702browse

基于PHP的简单采集数据入库程序,php采集入库

说到采集,无非就是远程获取信息->提取所需内容->分类存储->读取->展示

也算是简单"小偷程序"的加强版吧

下面是对应核心代码(别拿去做坏事哦^_^)

所要采集的内容是某游戏网站上的公告,如下图:

可先利用file_get_contents和简单正则获取基本页面信息

整理下基本信息,采集入库:

<&#63;php
  include_once("conn.php");


   if($_GET['id']<=8&&$_GET['id']){
     $id=$_GET['id'];
    $conn=file_get_contents("http://www.93moli.com/news_list_4_$id.html");//获取页面内容
  
  $pattern="/<li><a title=\"(.*)\" target=\"_blank\" href=\"(.*)\">/iUs";//正则

  preg_match_all($pattern, $conn, $arr);//匹配内容到arr数组

  //print_r($arr);die;
  
  foreach ($arr[1] as $key => $value) {//二维数组[2]对应id和[1]刚好一样,利用起key
    $url="http://www.93moli.com/".$arr[2][$key];
    $sql="insert into list(title,url) value ('$value', '$url')";
    mysql_query($sql);

    //echo "<a href='content.php&#63;url=http://www.93moli.com/$url'>$value</a>"."<br/>";  
  }
   $id++;
   echo "正在采集URL数据列表$id...请稍后...";
   echo "<script>window.location='list.php&#63;id=$id'</script>";

 }else{
   echo "采集数据结束。";
 }

&#63;>

conn.php是数据库连接文件

list.php是本页面

由于要采集的数据是分页显示的,且页面地址是规律递增,所以我用了js跳转代码,利用id传值控制采集的页数,也避免了for循环数目过大。

轻轻松松数据入库,下篇文章写关于具体url采集信息的过程。

PHP采集入库问题

mysql_connect() //先连接你的数据库
mysql_select_db() //选择你的数据库
mysql_query("insert into 你的表 (地址,标题) values ('$tmp[1][$i]',$tmp[2][$i])");//OK了,搞定!
 

PHP采集入库问题

php 里有$nr = implode(‘#’,$arr) 方法 ,就可以 了
不过上面的组成的是“内容1#内容2”,没有最后面的一个#,要是必须的话
就是$nr = implode('#',$arr).'#'

在笨的方法,就是用
foreach( $arr as $vl){
$nr.=$vl."#";
}
参考资料:$
 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn