PHP Book website Collection Example Tutorial

Source: Internet
Author: User
Tags date count modify php book trim
On the internet to see a lot of simple collection of tutorials, especially for the book site more, but not many examples, in view of a eight Chinese web crawl analysis, decided to this site, write a simple crawl tutorial, and with examples. Because I am lazy, many of the analysis is from the "use of PHP to make a simple content collector," I just further optimize his process, and completed the code instance of the writing.
The acquisition process is not difficult to do, as long as the analysis of the process, and then use the appropriate regular to get the content you want it. Nonsense not to say, the tutorial begins:
1. Analysis Portal:
Open a few more books, you can find the basic format of the title is: http://www.86zw.com/Book/ISBN/index.aspx. And it came to this:

Code:
$BookId = ' 1888 ';
$index = "http://www.86zw.com/Book/". $BookId. " /index.aspx ";//combination Bibliography Home URL
2. Open the page:

Code:
$contents =file_get_contents ($index);
3. Grab the Book Information page:

Code:
Grab book related information
Preg_match_all ("/<div id=\" crbooktitle\ "><span class=\" booktitle\ "> (. *) <\/span><\/div>/ Is ", $contents, $Arraytitle);
Preg_match_all ("/" <a href=\) (. *) \ ><font color=\ "#CC0000 \" > Click Read <\/font><\/a> "/is", $ Contents, $Arraylist);
Unset ($contents);
$title = $Arraytitle [1][0];//title
$list = "http://www.86zw.com". Trim ($Arraylist [1][0]);//List page URL
4. Create the Save directory and file:

Code:
Generate text Document name
$txt _name= $title. " TXT ";
Creatdir ($BookId);//Create a picture folder
Writestatistic ($title. " \ r \ n ", $txt _name);//book title write to text file
5. Enter the list page:

Code:
Go to List page
$list _contents=file_get_contents ($list);
6. Crawl List Page Chapter:

Code:
Go to List page
Sub-section grasping block
Preg_match_all ("|<div id=\" nclasstitle\ "> (. *)" <a href=\ "(. *) \ > Volume reading <\/a> <\/div> (. *) < Div id=\ "listend\" ><\/div>| Uis ", $list _contents, $Block);
Calculate the total number of chapters
$regcount =count ($Block [0]);
7. Sub-chapters for crawling:

Code:
Enter Chapter
for ($pageBookNum =0; $pageBookNum < $regcount; $pageBookNum + +) {
Unset ($Zhang);
unset ($list _url);
$Zhang = $Block [1][$pageBookNum];//Chapter Title
Writestatistic (' chapters: '). ( $pageBookNum + 1). ' '. $Zhang. " \ r \ n ", $txt _name);//Chapter header write to text file
Preg_match_all ("|<li><a href=\" (. *) \ title=\ (. *) \ > (. *) <\/a><\/li>| Uis ", $Block [3][$pageBookNum], $list _url);
Enter page
For ($ListNum =0 $ListNum <count ($list _url[1]); $ListNum + +) {
unset ($Book _url);
Unset ($Book);
unset ($Book _contents);
unset ($Book _time);
unset ($Book _title);
Update information $Book _time= $list _url[2][$ListNum];//Small section
$Book _title= $list _url[3][$ListNum];//Small chapter title
$Book _url=preg_replace ("' Index.shtm ' si", $list _url[1][$ListNum], $list);//small Chapter link URL
Writestatistic ($ListNum + 1). $Book _title. '-$Book _time. " \ r \ n ", $txt _name);//Small chapter title write to text file
$Book =file_get_contents ($Book _url);
Grab book Content
Preg_match_all ("/<div id=\" booktext\ "> (. *) <iframe id=hkwxc/is", $Book, $Arraycontents);
$Book _contents=preg_replace ("|<div style= ' Display:none ' >.*<\/div>| Uis ", ', $Arraycontents [1][0]);
$Book _contents=preg_replace ("|<br/>| Uis ", ' \n\r ', $Book _contents);
$Book _contents=preg_replace ("|<br>| Uis ", ' \n\r ', $Book _contents);
$Book _contents=preg_replace ("| | Uis ", ', $Book _contents);
$Book _contents=strip_tags ($Book _contents);
Judge Picture Page
if (Preg_match ("/<div align=\" Center\ ">/i ", $Book _contents)) {
Take picture URL
Preg_match_all ("|<div align=\" Center\ "><\/div>| Uis ", $Book _contents, $images);
Take pictures
For ($ImageNum =0 $ImageNum <count ($images [1]); $ImageNum + +) {
unset ($Image _url);
$Image _url= "http://www.86zw.com". Trim ($images [1][$ImageNum]);
$New _url= ' image/'. $BookId. ' /'. Time (). '. GIF ';
Copy a picture and generate a picture connection
if (copy ($Image _url, $New _url)) {
$Book _contents.= "}
}//Take picture End
}//Picture Judgment End

Writestatistic ($Book _contents, $txt _name);//content written to text file
}//page Loop End
}//Chapter Loop End
Two functions to use:

Code:
/**
* Write within the specified file package
*
* Parameters: String $sql: What is written
String $txt _name: Specify file name
* Return: void
* Scope: Public
* Date: 2007-11-29
*/
function writestatistic ($sql, $txt _name) {
$filename = "txt_packet/". $txt _name;//Note Modify the path of the file
if (file_exists ($filename)) {
$FP =fopen ($filename, "a");
}else{
$FP =fopen ($filename, "w");
}

$text = $sql;
Fwrite ($fp, $text);
Fclose ($FP);
}
/**
* Create Folder
*
* Parameter: String $BookId: Specify folder name
* Return: void
* Scope: Public
* Date: 2007-11-29
*/
function Creatdir ($BookId) {
$filename = "image/". $BookId//Note Modify the path to the file
if (!file_exists ($filename)) {
mkdir ($filename, 0777);
}
}
Complete a simple collection of books from here.
To write this is to give a simple example of phper to understand the collection, the collection is actually very simple ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.