Copy codeThe Code is as follows:
// Collect 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;
}
// Obtain the title and url
$ String =
Getwebcontent ('HTTP: // www. ***. com/learn/zhunbeihuaiyun/jijibeiyun/2 ');
// Regular expression match <li> get the 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. ***. com/learn/article/". $ out [$ key] [1];
}
// Obtain the document content based on the url
Foreach ($ article ['link'] as $ key => $ value ){
$ Content_html = getwebcontent ($ article ['link'] [$ key]);
Preg_match ("/<div id = pagenum_0 (. *)> [\ s | \ S] *? <\/Div>/", $ content_html, $ matches );
$ Article [content] [$ key] = $ matches [0];
}
// 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]);
}
?>