php 文章采集正則代碼
更新時間:2009年12月28日 00:07:38 作者:
php 文章采集代碼主要是應(yīng)用了正則表達式。
復(fù)制代碼 代碼如下:
//采集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;
}
//獲得標題和url
$string =
getwebcontent('http://www.***.com/learn/zhunbeihuaiyun/jijibeiyun/2');
//正則匹配<li>獲取標題和地址
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];
}
//根據(jù)url獲取文章內(nèi)容
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];
}
//不轉(zhuǎn)碼還真不能保存成文件
foreach($article[title] as $key=>$value){
$article[title][$key] = iconv('utf-8', 'gbk', $value);//轉(zhuǎn)碼
}
//存入文件
$num = count($article['title']);
for($i=0; $i<$num; $i++){
file_put_contents("{$article[title][$i]}.txt", $article['content'][$i]);
}
?>
相關(guān)文章
PHP數(shù)組 為文章加關(guān)鍵字連接 文章內(nèi)容自動加鏈接
PHP給文章加關(guān)鍵字連接,像163文章內(nèi)容自動加鏈接效果,其實很多php網(wǎng)站管理系統(tǒng)里面都有,可以參考里面的代碼。2011-12-12
PHP使用SOAP擴展實現(xiàn)WebService的方法
這篇文章主要介紹了PHP使用SOAP擴展實現(xiàn)WebService的方法,結(jié)合實例形式較為詳細的分析了SOAP擴展的原理及實現(xiàn)WebService的相關(guān)技巧,需要的朋友可以參考下2016-04-04

