Many people are also using PHPSimpleDom on the internet. although the efficiency is a little slow, the effect is still good, from including the class library file to the txt file written in about 78 seconds. The code is as follows:
// Contains the PHP Simple html Dom class library file
Include_once ('./simplehtmldom/simple_html_dom.php ');
Foreach ($ out as $ key => $ value ){
$ Article ['title'] [] = $ out [$ key] [2];
$ Article ['link'] [] = "http://www.babytree.com/learn/article/". $ out [$ key] [1];
}
// Obtain the document content based on the url
Foreach ($ article ['link'] as $ key => $ value ){
$ Html = file_get_html ($ value );
$ P = $ html-> find ('P [id = pagenum_0] ');
$ Article [content] [] = $ p [0]-> innertext;
}
// Title transcoding-this step is not required for real use-because we would have used utf8
// It cannot be saved as a file without transcoding.
Foreach ($ article [title] as $ key => $ value ){
$ Article [title] [$ key] = iconv ('utf-8', 'gbk', $ value); // transcoding
}
// Save the file
$ Num = count ($ article ['title']);
For ($ I = 0; $ I <$ num; $ I ++ ){
File_put_contents ("registry.article=title={ I }.txt", $ article ['content'] [$ I]);
}
/* It was originally intended to be sent before .. But let's take a look. it's all half past three... Even yesterday's
It is best to use regular expressions to obtain the content of an article, and the speed is also the fastest,
But regular expressions are good, but regular expressions are really difficult! So I checked it,
There are also many people using PHP Simple Dom on the Internet, although the efficiency is slow, but the effect is still good
From the file containing the class library to the txt file, it may take about 7/8 seconds for further optimization, especially the regular expression used to get the content of the article, which is too disgusting.
You can try it out */
?>
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