Php实现文章采集功能
数据采集,大部分是用正则表达式,我简单地介绍下如何实现采集的思路.这里说的是php的实现.一般是本机运行,放到空间上是不明智的,因为不但很耗资源还需要支持远程抓取函数,比如file_get_contents($urls)file($url)等.
1,文章列表页面的自动切换,以及文章路径的获得.
2,获得:标题,内容
3,入库
4,问题
1,文章列表页面的自动切换,以及文章路径的获得.
a,列表页面的自动切换一般依赖动态页面来实现.比如
http://www.phpfirst.com/foru ... d=1&page=$i
可以在后面利用$i的自动增加或范围来实现,比如$i++;
也可以像penzi演示的那个一样,要从第几页到第几页,代码方面控制$i的范围就可以.
b,文章路径的获得分需要填正则和无需填正则2种:
1)无需填正则就是获得上面的文章列表页面的所有连接
但是最好对连接进行过滤,处理---判断重复连接,只留一个,处理相对路径,变成绝对路径.比如../ 和./等.
以下是我写的乱七八糟的实现函数:
PHP 代码:
<? //$e=clinchgeturl("http://phpfirst.com/forumdisplay.php?fid=1");
//var_dump($e); function clinchgeturl($url) {
//$url="http://127.0.0.1/1.htm"; //$rootpath="http://fsrootpathfsfsf/yyyyyy/"; //var_dump($rrr); if(eregi(’(.)*[\.](.)*’,$url)){ $roopath=split("\/",$url); $rootpath="http://".$roopath[2]."/"; $nnn=count($roopath)-1;for($yu=3;$yu<$nnn;$yu++){$rootpath.=$roopath[$yu]."/";} // var_dump($rootpath); //http: ,’’,127.0.0.1,xnml,index.php } else{$rootpath=$url; //var_dump($rootpath); } if(isset($url)){ echo "$url 有下列裢接:<br>"; $fcontents = file($url); while(list(,$line)=each($fcontents)){ while(eregi(’(href[[:space:]]*=[[:space:]]*"?[[:alnum:]:@/._-]+[\?]?[^\"]*"?)’,$line,$regs)){ //$regs[1] = eregi_replace(’(href[[:space:]]*=[[:space:]]*\"?)([[:alnum:]:@/._-]+)(\"?)’,"\\2",$regs[1]); $regs[1] = eregi_replace(’(href[[:space:]]*=[[:space:]]*[\"]?)([[:alnum:]:@/._-]+[\?]?[^\"]*)(\.*)[^\"\/]*([\"]?)’,"\\2",$regs[1]);
if(!eregi(’^http:\/\/’,$regs[1])){
if(eregi(’^\.\.’,$regs[1])){ // $roopath=eregi_replace(’(http:\/\/)?([[:alnum:]:@/._-]+)[[:alnum:]+](\.*)[[:alnum:]+]’,"http:\/\/\\2",$url);
$roopath=split("\/",$rootpath); $rootpath="http://".$roopath[2]."/"; //echo "这是根本d :"."\n"; $nnn=count($roopath)-1;for($yu=3;$yu<$nnn;$yu++){$rootpath.=$roopath[$yu]."/";} //var_dump($rootpath); if(eregi(’^\.\.[\/[:alnum:]]’,$regs[1])){ //echo "这是../目录/ :"."\n"; //$regs[1]="../xx/xxxxxx.xx"; // $rr=split("\/",$regs[1]); //for($oooi=1;$oooi<count($rr);$oooi++) $rrr=$regs[1]; // {$rrr.="/".$rr[$oooi]; $rrr = eregi_replace("^[\.][\.][\/]",’’,$rrr); //}
$regs[1]=$rootpath.$rrr;
}
}else{ if(eregi(’^[[:alnum:]]’,$regs[1])){ $regs[1]=$rootpath.$regs[1]; }
else{ $regs[1] = eregi_replace("^[\/]",’’,$regs[1]); $regs[1]=$rootpath.$regs[1]; }
}
}
$line = $regs[2]; if(eregi(’(.)*[\.](htm|shtm|html|asp|aspx|php|jsp|cgi)(.)*’,$regs[1])){ $out[0][]=$regs[1]; } } } }for ($ouou=0;$ouou<count($out[0]);$ouou++) { if($out[0][$ouou]==$out[0][$ouou+1]){ $sameurlsum=1; //echo "sameurlsum=1:"; for($sameurl=1;$sameurl<count($out[0]);$sameurl++){ if($out[0][$ouou+$sameurl]==$out[0][$ouou+$sameurl+1]){$sameurlsum++;} else{break;} }
for($p=$ouou;$p<count($out[0]);$p++) { $out[0][$p]=$out[0][$p+$sameurlsum];} } }
$i=0; while($out[0][++$i]) { //echo $root.$out[0][$i]."\r\n"; $outed[0][$i]=$out[0][$i];
} unset($out); $out=$outed; return $out; } ?>
上面的东西只能zend,不然有碍市容:(
得到所有唯一的连接后,放到数组
2)需要填正则的处理
如果要准确地获得需要的文章连接,就用这个办法
按照ketle的思路
用
PHP 代码:
-------------------------------------------------------------------------------- function cut($file,$from,$end){ $message=explode($from,$file); $message=explode($end,$message[1]); return $message[0]; } $from是列表前面的html代码 $end是列表后面的html代码 以上可以通过表单提交参数. 对列表页面不是列表的部分去除,剩下的是需要的连接, 只要再通过下面正则得到: PHP: -------------------------------------------------------------------------------- preg_match("/^(http:\/\/)?(.*)/i", $url, $matches); return $matches[2];
2,获得:标题,内容
a首先,利用得到的文章路径,读取目标路径
可以通过以下函数:
PHP 代码:
-------------------------------------------------------------------------------- function getcontent($url) { if($handle = fopen ($url, "rb")){ $contents = ""; do { $data = fread($handle, 2048); if (strlen($data) == 0) { break; } $contents .= $data; } while(true); fclose ($handle); } else exit("........"); return $contents; }
或者直接
PHP 代码:
-------------------------------------------------------------------------------- file_get_contents($urls);
后者比较方便,但是缺点对比上面的就知道.
b,接下来得到标题:
一般用这个实现:
PHP 代码:
-------------------------------------------------------------------------------- preg_match("|<TITLE>(.*)</TITLE>|",$allcontent,$title);
里面的<TITLE>(.*)</TITLE>部分通过提交表单得到.
也可以通过一系列的cut函数
比如上面提过的function cut($file,$from,$end),具体的字符串切割可以通过字符处理函数切割实现,后面"取得内容"详细讲.
c,取得内容
取得内容方面和取得标题思路一样但情况比较复杂,因为内容附近不会是<title></title>这么简单.
1)内容附近的特征字符串有双引号,空格,换行符号等是大障碍
双引号需要变成 \" 可以通过addslashes()处理
换行符号要去掉 可以通过
PHP 代码:
-------------------------------------------------------------------------------- $a=ereg_replace("\n", ’’, $a); $a=ereg_replace("\r", ’’, $a);
去掉.
2)思路2,利用一大堆切割相关的函数对内容提取,需要大量的实践,调试,我正在弄这里,没有获得什么突破~~~~~~~~
3,入库
a,切保你的数据库可以插入
比如我的可以这样直接插入:
PHP 代码:
-------------------------------------------------------------------------------- $sql="INSERT INTO $articles VALUES (’’, ’$title’, ’’, ’$article’,’’, ’’, ’clinch’, ’from’, ’关键词’, 1, ’$栏目id’, ’$time’, 1);\r\n\r\n";
其中
PHP:
--------------------------------------------------------------------------------
(’’,
是自动升序的文章id,要确定升序的字段只有一个,比如pa是标题一个表,一个索引,内容一个表一个索引,两表的自动升序的字段要实时判断的,要插入比较麻烦,被认为"不可插".
b,sql文本导出
我的思路是:
在函数里return
PHP 代码:
-------------------------------------------------------------------------------- "INSERT INTO article_articles VALUES (’’, ’$title’, ’’, ’$body’,’’, ’’, ’clinch’, ’sohu’, ’php’, 1, ’#clinchall#’, ’$time’, 1);\r\n\r\n";
然后不断积累查询语句内容,最后
PHP 代码:
-------------------------------------------------------------------------------- $fname="what_"."_page".$i.".sql"; $fp = fopen("data/$f", ’w’); if(fputs($fp, $sql)) echo "file successfully write"; fclose($fp);
根据小新的思路,非常不错
PHP 代码:
-------------------------------------------------------------------------------- if(!empty($r_article) AND !empty($r_title)) { $time=time(); $r_title=addslashes(trim($r_title)); $r_article=addslashes($r_article);
$get_sql="INSERT INTO article_articles VALUES (’’, ’$r_title’, ’’, ’$r_article’,’’, ’’, ’’, ’sohu’, ’’, 1, ’’, ’$time’, 1);\r\n\r\n"; if(file_exists("xingfu_".$_SESSION[’p’].".sql")) { if(filesize("xingfu_".$_SESSION[’p’].".sql")>$mysize) { $_SESSION[’p’]++; } } $fp=fopen("xingfu_".$_SESSION[’p’].".sql",’a’); fwrite($fp,$get_sql); fclose($fp); 先判断是否得到标题和内容,然后生成sql语句放到文本中,还可以判断大小分页生成. c.直接入库(入mysql) 找个db_mysql类后 直接 PHP: [Copy to clipboard] -------------------------------------------------------------------------------- $DB->query("INSERT INTO `xxx` ( `xx` , `title` , `texts` , `sortid` ) VALUES (’’, ’".addslashes($title)."’, ’".addslashes($text)."’, ’".$mm."’)");
4,问题
a,其实采集大多需要具体问题具体分析,想做通用的很难.
换句话说,要做到让不懂正则的人随便采集,那可需要高手的后台实现.
b,最大问题是内容切割部分,因为html代码具体情况很复杂,需要调试多次.希望有人做出个类来.
c,当切不到时处理不好会中断,注意容错.用SESSION控制可以避免很多麻烦.当 timeout的时候,刷新下就ok了.延长脚本运行时间可以@set_time_limit(0)
|