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:
<?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?url=http://www.93moli.com/$url'>$value</a>"."<br/>"; } $id++; echo "正在采集URL数据列表$id...请稍后..."; echo "<script>window.location='list.php?id=$id'</script>"; }else{ echo "采集数据结束。"; } ?>
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.
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!
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: $