Analytics

วันจันทร์ที่ 4 กุมภาพันธ์ พ.ศ. 2556

php Robot Crawler สำหรับเข้าไปเก็บรูปในเว็ปอื่น



ตัว robot ที่สร้างขึ้นเพื่อเก็บข้อมูลในเว็ปอื่น เป็นโครงสร้างพื้นฐานของ App EasyBuy ในลักษณะเดียวกับของ FlipBoard โดย code ตั้งต้นมีดังนี้


<?

// include SBot class
require_once 'lib/SBot.php';

function urlcut($u)
{
   while (strrpos($u,'?'))
   {
     $u=substr($u,0,strrpos($u,'?'));
   }
   $p1=strrpos($u,'.');
   $p2=strrpos($u,'/');
   if ($p2<$p1)
   { 
     $u=substr($u,0,strrpos($u,'/')+1);
   }
   return $u;
}

function addurl($l,$uu)
{
   if (strtoupper(substr($l,0,11))=='<IMG SRC="/' or
       strtoupper(substr($l,0,11))=="<IMG SRC='/"  )
   {
   if (strtoupper(substr($l,0,11))=='<IMG SRC="/')
   {
     $r = '<IMG SRC="' . $uu . substr($l,11);
   } else
   {
     $r = "<IMG SRC='" . $uu . substr($l,11);
     } 
   } else
   {
     $r = $l;
   }
   return $r;
}

// Get picture url from tag <IMG SRC="..">
function pictureurl($l)
{
   $c = substr($l,10); // this line cut <IMG SRC=?
   if (strpos($c,"'"))
   {
     $c = substr($c,0,strpos($c,"'"));
   } else
   {
     $c = substr($c,0,strpos($c,'"'));
   }
   return $c;
}


function checksize($l)
{
  if (strpos(strtolower($l),'width='))
  {
     $c = strtolower($l);
     $c = substr($c,strpos($c,'width=')+7); //+7 because width=? and quotes symbol
     if (strpos($c,' '))
     {
       $c = substr($c,0,strpos($c,' ')-1); //-1 for shift to the front of the quotes symbol
     }
     if (strpos($c,'>'))
     {
       $c = substr($c,0,strpos($c,'>')-1); //-1 for shift to the front of the quotes symbol
     }
  }
  list($width, $height, $type, $attr) = getimagesize(pictureurl($l));
  if ($c > $width)
  {
     return $c; 
  } else
  {
     return $width;
  }
}

// initialize SBot object
$bot = new SBot();

// URL to crawl
$url = 'http://podgarden.tarad.com/index.php?lang=th';

// crawl URL
$bot->crawl($url);

// check for successful crawl
if($bot->isSuccess()) {
      // successful crawl, display crawl data
      $j = 0;   
      $rawdata = str_split($bot->getData(),200);
      $data = '';
      $line = array(' ');
      $i = 1;
      while ($rawdata[$j])
      {
      $data = $data . $rawdata[$j];
      $pos = strpos($data,'<');
      $pos1= strpos($data,'>');
      while ($pos or $pos1)
      {
          while ($pos1<$pos)
          {
        array_push($line,substr($data,0,$pos1+1));
        $i++;
        $data = substr($data,$pos1+1);
            $pos = strpos($data,'<');
            $pos1= strpos($data,'>');
          }
          if ($pos > 0)
          {
        array_push($line,substr($data,0,$pos));
        $i++; 

          }
          array_push($line,substr($data,$pos,($pos1-$pos)+1));
          $i++;
          $data = substr($data,$pos1+1);
        $pos = strpos($data,'<');
        $pos1= strpos($data,'>');
      }
      $j++;
      }
      array_push($line,$data);

      $uu = urlcut($url);
      echo $uu;
      echo 'Total size:' . sizeof($line) . ' lines<br>';
      $i = 1;
      $l = 0;
      $t = 100;
      while ($i <= sizeof($line))
      {
        if (strtoupper(substr($line[$i],0,8))=='<IMG SRC')
        {
        $p = addurl($line[$i],$uu);
        $s = checksize($p);
    if ($s>30)
        {
          echo '<div style="position:absolute; left:'.$l.'px; top:'.$t.'px; background-color:Gold;width:'. $s .'px;height:200px;z-index:2">';
      echo $p;
          echo ' Size :' . $s;
          echo '</div>';
          $l=$l+$s;
          if ($l>800)
          {
             $l=0;
             $t=$t+200;
          }
        }
        }
        $i++;
      }     

// failed to crawl successfully
} else {
      // display error
      echo 'Crawl Error: ' . $bot->getErrorLast();
}

?>








สำหรับ code ในส่วน Sbot.php ได้นำมาจากเว็ป ของ Mr.Shay Anderson ต้องขอพระเจ้าอวยพรเขาไว้ ณ ที่นี้

http://www.shayanderson.com/php/php-robot-for-crawling-urls-and-data-harvesting.htm


สำหรับข้
อมูลในส่วนการใช้ div นำมาจาก
http://www.divland.com/blog/2007/05/06/position/

อพระเจ้าอวยพรเขาเช่นกัน

ไม่มีความคิดเห็น:

แสดงความคิดเห็น