複製程式碼 程式碼如下:
//擷取html
function getwebcontent($url){
$html
function getwebcontent($url){ $html ();
$timeout = 10;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setop( );
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
$contents = trim(curl_exec($ch));
curl_close($ch);
return $contents}
curl_close($ch);
return $contents>;
//獲得標題和url
$string =
getwebcontent('http://www.***.com/learn/zhunbeihuaiyun/jijibeiyun/2');
//正規匹配
取得標題與地址
preg_match_all ("/
(.*)/",$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];
}
//依url取得文章內容
foreach($article['link'] as $key=>$value){
$content_html = getwebcontent($article['link'][$key]);
preg_match("/[s|S]*?
/",$content_html,$matches);
$article[content][$key] = $matches[0];
}
//不轉碼還真不能儲存成檔案
foreach($article[title] as $key=>$value){
$article[title][$key] = iconv(' utf-8', 'gbk', $value);//轉碼
}
//存入檔案
$num = count($article['title']);
for ($i=0; $ifile_put_contents("{$article[title][$i]}.txt", $article['content'][$i]);
} ?>
以上就介紹了採集 php 文章採集正規程式碼,包括了採集方面的內容,希望對PHP教程有興趣的朋友有所幫助。