<?php
Contains PHP simple HTML Dom class library files
Include_once ('./simplehtmldom/simple_html_dom.php ');
Capturing 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 <li> get title and address
Preg_match_all ("/<li><a href="/learn/article/(. *) "> (. *) </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 this step--because we were going to use UTF8.
I can't save it as a file without transcoding.
foreach ($article [title] as $key => $value) {
$article [title][$key] = iconv (' utf-8 ', ' GBK ', $value);//transcoding
}
Deposit in File
$num = count ($article [' title ']);
for ($i =0; $i < $num; $i + +) {
File_put_contents ("{$article [title][$i]}.txt", $article [' content '] [$i]);
}
* I would have sent it before 12 o'clock. But look down on it all 3:30 ... Even if it was yesterday.
It is the best to get the content of the article, the speed is the fastest,
But the positive is good, but the regular expression is really difficult! So a little check,
There are also a lot of people on the Web using PHP simple Dom Although the efficiency is slow, but the effect is good
From the containing class library file to the write TXT file is about 7/8 seconds and with further optimizations, especially when it comes to getting the content of the article, that's disgusting.
We can do a little research on the * *
?>