PHP头条
热点:

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 有下列裢接:
";

  $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 :"." ";

  $nnn=count($roopath)-1;for($yu=3;$yu<$nnn;$yu++){$rootpath.=$roopath[$yu]."/";}

  //var_dump($rootpath);

  if(eregi(^..[/[:alnum:]],$regs[1])){

  //echo "这是../目录/ :"." ";

  //$regs[1]="../xx/xxxxxx.xx";

  // $rr=split("/",$regs[1]);

  //for($oooi=1;$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

  {

  if($out[0][$ouou]==$out[0][$ouou+1]){

  $sameurlsum=1;

  //echo "sameurlsum=1:";

  for($sameurl=1;$sameurl

  if($out[0][$ouou+$sameurl]==$out[0][$ouou+$sameurl+1]){$sameurlsum++;}

  else{break;}

  }

  for($p=$ouou;$p

  { $out[0][$p]=$out[0][$p+$sameurlsum];}

  }

  }

  $i=0;

  while($out[0][++$i]) {

  //echo $root.$out[0][$i]." ";

  $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("||",$allcontent,$title);

  里面的部分通过提交表单得到.

  也可以通过一系列的cut函数

  比如上面提过的function cut($file,$from,$end),具体的字符串切割可以通过字符处理函数切割实现,后面"取得内容"详细讲.

  c,取得内容

  取得内容方面和取得标题思路一样但情况比较复杂,因为内容附近不会是这么简单.

  1)内容附近的特征字符串有双引号,空格,换行符号等是大障碍

  双引号需要变成 "  可以通过addslashes()处理

  换行符号要去掉 可以通过

  PHP:

  --------------------------------------------------------------------------------

  $a=ereg_replace(" ", , $a);

  $a=ereg_replace("", , $a);

  去掉.

  2)思路2,利用一大堆切割相关的函数对内容提取,需要大量的实践,调试,我正在弄这里,没有获得什么突破~~~~~~~~

  3,入库

  a,切保你的数据库可以插入

  比如我的可以这样直接插入:

  PHP:

  --------------------------------------------------------------------------------

  $sql="INSERT INTO $articles VALUES (, $title, , $article,, , clinch, from, 关键词, 1, $栏目id, $time, 1); ";

  其中

  PHP:

  --------------------------------------------------------------------------------

  (,

  是自动升序的

www.phpzy.comtrue/phprm/25557.htmlTechArticlePHP中怎样实现文章采集 数据采集,大部分是用正则表达式,我简单地介绍下如何实现采集的思路.这里说的是php的实现.一般是本机运行,放到空间上是不明智的,因为不但很耗资源还需...

相关文章

    暂无相关文章

PHP之友评论

今天推荐