Copy CodeThe code is as follows:
Contains PHP simple HTML Dom class library file
Include_once ('./simplehtmldom/simple_html_dom.php ');
Collecting HTML
function Getwebcontent ($url) {
$ch = Curl_init ();
$timeout = 10;
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
curl_setopt ($ch, curlopt_followlocation, 1);
$contents = Trim (curl_exec ($ch));
Curl_close ($ch);
return $contents;
}
Get title and URL
$string =
Getwebcontent (' HTTP://WWW.BABYTREE.COM/LEARN/ZHUNBEIHUAIYUN/JIJIBEIYUN/2 ');
Regular match
Get title and Address
Preg_match_all ("/
(. *) <\/a>/",
$string, $out, Preg_set_order);
foreach ($out as $key = = $value) {
$article [' title '] [] = $out [$key][2];
$article [' link '] [] = "http://www.babytree.com/learn/article/". $out [$key][1];
}
Get article content based on URL
foreach ($article [' link '] as $key = + $value) {
$html = file_get_html ($value);
$div = $html->find (' div[id=pagenum_0] ');
$article [content][] = $div [0]->innertext;
}
Title Transcoding---Really use it without this step--because we're going to use UTF8.
No transcoding is really not saved as a file
foreach ($article [title] as $key = + $value) {
$article [title][$key] = iconv (' utf-8 ', ' GBK ', $value);//transcoding
}
Deposit file
$num = count ($article [' title ']);
for ($i =0; $i < $num; $i + +) {
File_put_contents ("{$article [title][$i]}.txt", $article [' content '] [$i]);
}
* * Originally wanted to send before 12 o'clock. But it's 3:30 to look down ... Even yesterday's.
It was best to get the article content, and the speed was the fastest,
But the regular expression is very difficult! So I looked up a little bit,
There are also many people on the Internet with PHP simple Dom Although the efficiency is slow, but the effect is good
From containing the class library file to writing the TXT file is about 7/8 seconds and taking it further, especially when it comes to getting the content of the article, that's disgusting.
You can study for a little bit * *
?>
The above on the introduction of simple is what the meaning of PHP simple_html_domphp+ is collecting the article code, including simple what is the meaning of the content, I hope that the PHP tutorial interested in a friend helpful.