Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2536734
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: 系统运维

2011-10-26 17:15:44

  

此信息采集程序的实现原理: 首先,给定一个文章列表页面的URL,通过crawl爬行抓去该页面中的article的URL; 然后,对同一个web应用的每一篇文章而言,其title,content都是可以定位到的。通过匹配取出其中内容。 此程序包含两个页面,保存为*.php后可以运行在SAE平台下,以下是详细代码:
crawler.php
  1. <h2>Please input a valid url to CRAWLING</h2>
  2. <form action="" method="post">
  3.     <input type="text" name="site" value="" style="width:200px;" />
  4.     <input type="submit" name="submit" value="CRAWLING" />
  5. </form>
  6. <?php
  7.   if(isset($_POST['submit'])){
  8.   $link_to_dig = $_POST['site'];
  9.   //$link_to_dig = "";

  10.   
  11.    $f = new SaeFetchurl();
  12.    $original_file = $f->fetch($link_to_dig);
  13.   //$original_file = file_get_contents($link_to_dig);
  14.   if(!$original_file)
  15.     die("Error loading {$link_to_dig}");

  16.   $path_info = parse_url($link_to_dig);
  17.   $base = $path_info['scheme'] . "://" . $path_info['host'];

  18.   $stripped_file = strip_tags($original_file, "");
  19.   $fixed_file = preg_replace("/]*)href=\"\//is", ", $stripped_file);
  20.   $fixed_file = preg_replace("/]*)href=\"\?/is", ", $fixed_file);
  21.   preg_match_all("/]*)href=\"([^\"]*)\"(?:[^>]*)>(?:[^<]*)<\/a>/is", $fixed_file, $matches);

  22.   

  23.   $result = print_r($matches, true);
  24.   $result = str_replace("<", "<", $result);
  25.   print "
    " . $result . "
    "
    ;
  26.  

  27. }

  28. ?>
gatherinformation.php
  1. <html >
  2.  <head>
  3.   <title>Gather information</title>
  4.  </head>
  5. <body>
  6. <h2>Gather information</h2>
  7. <form action="" method="post">
  8.     <table style="width:800px;">
  9.      <tr><th>URL:</th><td ><input type="text" name="url" value="" style="width:400px;" />increase tid if you like</td></tr>
  10.      <tr><th>Start:</th><td><input type="text" name="start" value="" style="width:400px;" />Input: <?php print htmlentities("
    "); ?></td></tr>
  11.      <tr><th>End:</th><td><input type="text" name="end" value="" style="width:400px;" />Input: <?php print htmlentities("
    ); ?></td></tr>
  12.      <tr><th></th><td><input type="submit" name="submit" value="Grasp" /></td></tr>    
  13.     </table>
  14.   <div>
  15.       <?php
  16.       
  17.        if(isset($_POST['submit'])){
  18.             $url = $_POST['url'];
  19.             $start = $_POST['start'];
  20.             $end = $_POST['end'];    
  21.             if ($source = file_get_contents($url)) {        
  22.                  //test();
  23.              echo extract_content($source,$start,$end);
  24.             }
  25. }
  26.       ?>
  27.   </div>
  28. </form>
  29. </body>
  30. </html>
  31. <?php

  32. function extract_content($string,$start,$end)
  33. {
  34.   $pos = stripos($string,$start);
  35.   $str1 = substr($string,$pos);
  36.   $str2 = substr($str1,strlen($start));
  37.   $second_pos = stripos($str2,$end);
  38.   $str3 = substr($str2,0,$second_pos);
  39.   $content = trim($str3);
  40.   return $content;
  41. }
  42. function getTextBetweenTags($tag, $html, $strict=0)
  43. {
  44.    
  45.     $dom = new DOMDocument('1.0', 'utf-8');
  46.     if($strict==1)
  47.     {
  48.         $dom->loadXML($html);
  49.     }
  50.     else
  51.     {
  52.         $dom->loadHTML($html);
  53.     }

  54.     /*** discard white space ***/
  55.     $dom->preserveWhiteSpace = false;
  56.     $content = $dom->getElementsByTagname($tag);
  57.     $out = array();
  58.     foreach ($content as $item)
  59.     {
  60.         $out[] = $item->nodeValue;
  61.     }
  62.     return $out;
  63. }

  64. function getTextBetweenComments($string, $start,$end)
  65. {
  66.     $pattern = "/<$start>(.*?)<\/$end>/";
  67.     preg_match($pattern, $string, $matches);
  68.     return $matches[1];
  69.  }
  70.  
  71.  function search($source,$start,$count){
  72.       return substr($source,strpos($source, $start)+strlen($start), (int)$count);
  73.  }
  74.  

  75. function test(){
  76.     $string ='Street23 -ParisStreet 33- Berlin Street 453- London';
  77.     $array = explode("",$string);
  78.     $desired_array = array();
  79.     foreach($array as $value)
  80.     {
  81.      $value = trim(strip_tags($value));;
  82.      if($value)
  83.      {
  84.      list($street,$city) = explode("-",$value);
  85.      $desired_array[trim($street)] = trim($city);    
  86.      }
  87.     }
  88.     echo "
    ";
  89.     print_r($desired_array);
  90.     echo "";
  91. }

  92. ?>

阅读(1936) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~