php 实现信息采集(网页内容抓取)程序代码

编程语言
0 842

  1. <?
  2. //采集首页地址
  3. $url="http://www.xz-src.com/";
  4. //获取页面代码
  5. $rs=file_get_contents($url);
  6. //设置匹配正则
  7. //$fp=fopen("text.txt","a");
  8. //$fw=fwrite($fp,$rs);
  9. //fclose($fp);
  10. /*<A
  11. href="http://www.xz-src.com/"
  12. target=_blank>留住你身边的好男人*/
  13. $preg='/<a\s+href=\"[^>]+\">(.*)/i';
  14. //进行正则搜索
  15. preg_match_all($preg,$rs,$title);
  16. //计算标题数量
  17. $count=count($title[0]);
  18. echo $count."
    ";
  19. //通过标题数量进行内容采集
  20. for ($i=0;$i<$count;$i++){
  21. //设置内容页地址
  22. $pr='/<a\s+href=\"[^>]+\">/isU';
  23. preg_match_all($pr,$title[0][$i],$jurl);
  24. $substr=substr($jurl[0][0],9);
  25. $curl=substr($substr,0,-18);
  26. //获取内容页代码
  27. $c=file_get_contents($curl);
  28. //设置内容页匹配正则
  29. $pc='/<a\s+href=\"[^>]+\">/i';
  30. //进行正则匹配搜索
  31. preg_match($pc,$c,$content);
  32. //输出标题
  33. echo $title[0][$i]."
    ";
  34. echo $title[1][$i]."
    ";
  35. $concount=count($content[0]);
  36. echo $concount."
    ";
  37. echo $content[0][0];
  38. for ($j=0;$j<$concount;$j++){
  39. }
  40. }
  41. ?>


    
    <?
    //采集首页地址
    $url="http://www.xz-src.com/";
    //获取页面代码
    $rs=file_get_contents($url);
    //设置匹配正则
    //$fp=fopen("text.txt","a");
    //$fw=fwrite($fp,$rs);
    //fclose($fp);
    /*留住你身边的好男人*/
    $preg='/<a\s+href=\"[^>]+\">(.*)/i';
    //进行正则搜索
    preg_match_all($preg,$rs,$title);
    //计算标题数量
    $count=count($title[0]);
    echo $count."
    "
    ; //通过标题数量进行内容采集 for ($i=0;$i<$count;$i++){ //设置内容页地址 $pr='/<a\s+href=\"[^>]+\">/isU'; preg_match_all($pr,$title[0][$i],$jurl); $substr=substr($jurl[0][0],9); $curl=substr($substr,0,-18); //获取内容页代码 $c=file_get_contents($curl); //设置内容页匹配正则 $pc='/<a\s+href=\"[^>]+\">/i'; //进行正则匹配搜索 preg_match($pc,$c,$content); //输出标题 echo $title[0][$i]."
    "
    ; echo $title[1][$i]."
    "
    ; $concount=count($content[0]); echo $concount."
    "
    ; echo $content[0][0]; for ($j=0;$j