Home > Backend Development > PHP Tutorial > Simple data collection and storage program based on PHP, php collection and storage_PHP tutorial

Simple data collection and storage program based on PHP, php collection and storage_PHP tutorial

WBOY
Release: 2016-07-13 10:21:42
Original
749 people have browsed it

Simple data collection and warehousing program based on PHP, php collection and warehousing

When it comes to collection, it is nothing more than obtaining information remotely ->Extracting the required content->Classified storage->Reading->Display

It can also be regarded as an enhanced version of the simple "thief program"

The following is the corresponding core code (don’t use it to do bad things^_^)

The content to be collected is an announcement on a game website, as shown below:

You can first use file_get_contents and simple regular expressions to obtain basic page information

Organize the basic information and collect it into the database:

<&#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;>
Copy after login

conn.php is the database connection file

list.php is this page

Since the data to be collected is displayed in pages, and the page address increases regularly, I used js jump code and used the id value to control the number of pages collected, which also avoided the number of for loops being too large.

Easily store data into the database. The next article will be about the process of collecting information from specific URLs.

PHP collection and warehousing problem

mysql_connect() //Connect to your database first
mysql_select_db() //Select your database
mysql_query("insert into your table (address, title) values ​​('$tmp[1][ $i]',$tmp[2][$i])");//OK, done!

PHP collection and warehousing problem

There is $nr = implode('#',$arr) method in php, that's it
But the above composition is "Content 1# Content 2" without the last #, if necessary
That’s $nr = implode('#',$arr).'#'

The stupid way is to use
foreach( $arr as $vl){
$nr .=$vl."#";
}
Reference: $

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/854356.htmlTechArticleA simple data collection and warehousing program based on PHP, PHP collection and warehousing. When it comes to collection, it is nothing more than obtaining information remotely - Extracting the required content - classifying storage - reading - display is also a simple "thief program...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template