 |
|
|  |
Á¤±ÔÇ¥Çö½ÄÀ» ÀÌ¿ëÇÑ ÆäÀÌÁöÆÄ½Ì ¿¹Á¦ Ãâó:À¯Ã¢È´Ô ºí·Î±× |
|
|
 |
11³â Àü |
ÆäÀÌÁöÆĽ̿¹Á¦ : ±×´©º¸µå ÃÖ±Ù °Ô½Ã¹°
¿¹Á¦ : http://chancein.com/svc/sample/page_parsing1.php
ÀÀ¿ë : http://chancein.com/svc/
step1:
±×´©º¸µåÀÇ ÃÖ±Ù °Ô½Ã¹° urlÀº http://sir.co.kr/bbs/new.php ÀÔ´Ï´Ù.
¸ÕÀú php¿¡¼ ÇØ´ç urlÀÇ ³»¿ëÀ» Àоî¿É´Ï´Ù.
Àоî¿À´Â ¹æ¹ýÀº ¿©·¯°¡Áö°¡ ÀÖ°ÚÀ¸³ª
°£´ÜÇÏ°Ô file_get_contents (http://kr.php.net/manual/en/function.file-get-contents.php)¸¦ »ç¿ëÇØ º¾´Ï´Ù.
¾Æ·¡¿Í °°Àº »ùÇÃÄڵ带 ÀÛ¼ºÇÏ¿©
À¥¿¡ ¾÷·Îµå ½ÃŲÈÄ È®ÀÎÇØ º¾´Ï´Ù.
<?php
$url = 'http://sir.co.kr/bbs/new.php';
$text = file_get_contents($url);
echo $text;
?>
Á¤»óÀûÀ¸·Î µÈ °æ¿ì´Â ½ºÅ©¸³Æ® ¿¡·¯¿Í ÇÔ²² ±úÁø ¸ð½ÀÀÇ ÃֱٰԽù° ÆäÀÌÁö°¡ º¸ÀÔ´Ï´Ù.
±×·¸°Ô º¸ÀÌ´Â °æ¿ì´Â ½ºÅ©¸³Æ® °æ·Î¿Í À̹ÌÁö °æ·Î°¡ ´Ù¸£±â ¶§¹®ÀÔ´Ï´Ù.
½Å°æ¾µ ÇÊ¿ä´Â ¾ø½À´Ï´Ù.
step2:
ÇØ´ç ÆäÀÌÁö¸¦ ¼Ò½º º¸±â ÇÕ´Ï´Ù.
ÆäÀÌÁöÆĽÌÀ» ÇϱâÀ§Çؼ± ÇØ´ç ÆäÀÌÁöÀÇ Æ¯Á¤ºÎºÐÀÌ ¾î¶²±¸Á¶·Î µÇ¾îÀÖ´ÂÁö ÆľÇÇÕ´Ï´Ù.
µ¥ÀÌŸ¸¦ °É·¯³»±â ÁÁ°Ô ÆäÀÌÁöÀÇ ³»¿ëÀ» ÇÊ¿äÇÑ ºÎºÐ¸¸ À߶ó³À´Ï´Ù.
$temp = @explode('<colgroup width="60">', $text);
$temp = @explode('</form>', $temp[1]);
$text = $temp[0];
echo $text;
¾Æ·¡¿Í °°Àº »ùÇÃÄڵ带 ÀÛ¼ºÇÏ¿©
À¥¿¡ ¾÷·Îµå ½ÃŲÈÄ È®ÀÎÇØ º¾´Ï´Ù.
<?php
$url = 'http://sir.co.kr/bbs/new.php';
$text = file_get_contents($url);
$temp = @explode('<colgroup width="60">', $text);
$temp = @explode('</form>', $temp[1]);
$text = $temp[0];
echo $text;
?>
ƯÁ¤ÇÊ¿äÇÑ ºÎºÐ¸¸ À߶ó¿Ó±â ¶§¹®¿¡ ¿ª½Ã ±úÁ®¼ º¸ÀÔ´Ï´Ù.
¿ª½Ã ½Å°æ¾µ ÇÊ¿ä´Â ¾ø½À´Ï´Ù.
step3:
ÇØ´ç ÆäÀÌÁö¸¦ ¼Ò½º º¸±â ÇÕ´Ï´Ù.
<tr align='center' height='30'><td align='left'><nobr style='display:block; overflow:hidden; width:75px;'><a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a></nobr></td><td align='left'><nobr style='display:block; overflow:hidden; width:75px;'><a href='new.php?bo_table=g4_qa&view=&sfl=mb_id&stx=&srows=25'>±×´©4Áú´ä</a></nobr></td><td align='left' style='padding:0 0 0 10px;'><nobr><a href='board.php?bo_table=g4_qa&wr_id=105530#c_105530'><span class='small'>[ÄÚ]</span> ¸µÅ©µÈ µ¥ÀÌÅ͸¦ ´Ù¿î¹ÞÀ¸·Á¸é,,,, </a></nobr></td><td><a href="javascript:;" onClick="showSideView(this, 'slk304', 'ÆĶûÆĶû', 'c2xrMzA0QG5hdGUuY29t', '');" title="[slk304]ÆĶûÆĶû"><span class='member'>ÆĶûÆĶû</span></a></td><td>17:38</td></tr>..........
ÀÌ·¯ÇÑ ºÎºÐÀÌ ÁÖ¿í ÀÖ½À´Ï´Ù.
<tr>·Î¼ ÇÑÁÙ¿¡ Çϳª¾¿ ÃÖ±Ù °Ô½Ã¹°ÀÌ ³ª¿µÇ¾îÀÖ½À´Ï´Ù.
ÇÑÁÙ¿¡¼ ¾òÀ» µ¥ÀÌŸ´Â ±×·ì¸í, °Ô½ÃÆǸí, Á¦¸ñ, ±Û¾´À̴г×ÀÓ À̶ó°í ÇÑ´Ù¸é
±×ºÎºÐ¿¡ ´ëÇÑ µ¥ÀÌŸ¸¦ ¾ò±â À§ÇØ preg_match¸¦ »ç¿ëÇÕ´Ï´Ù.
±×·ì¸í¿¡ ÇØ´çÇÏ´Â ºÎºÐÀº <td align='left'><nobr style='display:block; overflow:hidden; width:75px;'><a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a></nobr></td> ¿ä·¸°Ô µÈºÎºÐÀÓÀ» ¾Ë¼ö ÀÖ½À´Ï´Ù. ÀÌÁß¿¡¼ ±×·ì¸í¸¸À» °¡Á®¿À±â À§Çؼ±
preg_match("`<a href='new\.php\?gr_id=.+'>(.+)</a>`iU", $text, $match);
print_r($match);
°á°ú¹°À» È®ÀÎÇغ¸¸é
Array
(
[0] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[1] => ±×´©º¸µå4
)
¿Í °°ÀÌ Á¦ÀÏ Ã¹¹ø°¿¡ ¸ÅÄ¡µÈ ³»¿ëÀÌ º¸ÀÔ´Ï´Ù.
preg_match_all("`<a href='new\.php\?gr_id=.+'>(.+)</a>`iU", $text, $match);
ÀÌ·¸°Ô Çϸé
ÇØ´ç $text¿¡ Æ÷ÇÔµÈ ±×·ì¸í¿¡ ´ëÇÑ ³»¿ëÀÌ ¸ðµÎ ¹è¿·Î¼ º¸¿©Áý´Ï´Ù.
Array
(
[0] => Array
(
[0] => <a href='new.php?gr_id=tip&view=&sfl=mb_id&stx=&srows=25'>ÆÁ&Å×Å©</a>
[1] => <a href='new.php?gr_id=community&view=&sfl=mb_id&stx=&srows=25'>Ä¿¹Â´ÏƼ</a>
[2] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[3] => <a href='new.php?gr_id=community&view=&sfl=mb_id&stx=&srows=25'>Ä¿¹Â´ÏƼ</a>
[4] => <a href='new.php?gr_id=community&view=&sfl=mb_id&stx=&srows=25'>Ä¿¹Â´ÏƼ</a>
[5] => <a href='new.php?gr_id=community&view=&sfl=mb_id&stx=&srows=25'>Ä¿¹Â´ÏƼ</a>
[6] => <a href='new.php?gr_id=community&view=&sfl=mb_id&stx=&srows=25'>Ä¿¹Â´ÏƼ</a>
[7] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[8] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[9] => <a href='new.php?gr_id=community&view=&sfl=mb_id&stx=&srows=25'>Ä¿¹Â´ÏƼ</a>
[10] => <a href='new.php?gr_id=community&view=&sfl=mb_id&stx=&srows=25'>Ä¿¹Â´ÏƼ</a>
[11] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[12] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[13] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[14] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[15] => <a href='new.php?gr_id=community&view=&sfl=mb_id&stx=&srows=25'>Ä¿¹Â´ÏƼ</a>
[16] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[17] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[18] => <a href='new.php?gr_id=community&view=&sfl=mb_id&stx=&srows=25'>Ä¿¹Â´ÏƼ</a>
[19] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[20] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[21] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[22] => <a href='new.php?gr_id=gnuboard4&view=&sfl=mb_id&stx=&srows=25'>±×´©º¸µå4</a>
[23] => <a href='new.php?gr_id=community&view=&sfl=mb_id&stx=&srows=25'>Ä¿¹Â´ÏƼ</a>
[24] => <a href='new.php?gr_id=community&view=&sfl=mb_id&stx=&srows=25'>Ä¿¹Â´ÏƼ</a>
)
[1] => Array
(
[0] => ÆÁ&Å×Å©
[1] => Ä¿¹Â´ÏƼ
[2] => ±×´©º¸µå4
[3] => Ä¿¹Â´ÏƼ
[4] => Ä¿¹Â´ÏƼ
[5] => Ä¿¹Â´ÏƼ
[6] => Ä¿¹Â´ÏƼ
[7] => ±×´©º¸µå4
[8] => ±×´©º¸µå4
[9] => Ä¿¹Â´ÏƼ
[10] => Ä¿¹Â´ÏƼ
[11] => ±×´©º¸µå4
[12] => ±×´©º¸µå4
[13] => ±×´©º¸µå4
[14] => ±×´©º¸µå4
[15] => Ä¿¹Â´ÏƼ
[16] => ±×´©º¸µå4
[17] => ±×´©º¸µå4
[18] => Ä¿¹Â´ÏƼ
[19] => ±×´©º¸µå4
[20] => ±×´©º¸µå4
[21] => ±×´©º¸µå4
[22] => ±×´©º¸µå4
[23] => Ä¿¹Â´ÏƼ
[24] => Ä¿¹Â´ÏƼ
)
)
À§ Á¤±ÔÇ¥Çö½Ä¿¡¼ i º¯°æÀÚ´Â ´ë¼Ò¹®ÀÚ¸¦ ±¸ºÐÇÏÁö ¾Ê°Ú´Ù´Â Àǹ̸¦ ³ªÅ¸³À´Ï´Ù.
±×¸®°í U´Â Áߺ¹ÀÌ ¾ø´Â ¹üÀ§³»¿¡¼ ³»¿ëÀ» °¡Á®¿À°Ú´Ù´Â Àǹ̸¦ ³ªÅ¸³À´Ï´Ù.
°Ô½ÃÆǸí, Á¦¸ñ, ±Û¾´À̴г×ÀÓ µî°ú °°Àº ³ª¸ÓÁö µ¥ÀÌŸµéµµ À§ÇÑ °°Àº ¹æ½ÄÀ¸·Î µ¥ÀÌŸ¸¦ ÃßÃâÇØ ³¾¼ö ÀÖ½À´Ï´Ù.
¾Æ·¡¿Í °°Àº »ùÇÃÄڵ带 ÀÛ¼ºÇÏ¿©
À¥¿¡ ¾÷·Îµå ½ÃŲÈÄ È®ÀÎÇØ º¾´Ï´Ù.
<?php
$url = 'http://sir.co.kr/bbs/new.php';
$text = file_get_contents($url);
$temp = @explode('<colgroup width="60">', $text);
$temp = @explode('</form>', $temp[1]);
$text = $temp[0];
//±×·ì¸íµé Å×½ºÆ® Ãâ·Â
preg_match_all("`<a href='new\.php\?gr_id=.+'>(.+)</a>`iU", $text, $match);
print_r($match);
//°Ô½ÃÆǸíµé Å×½ºÆ® Ãâ·Â
preg_match_all("`<a href='new\.php\?bo_table=.+'>(.+)</a>`iU", $text, $match);
print_r($match);
//Á¦¸ñµé Å×½ºÆ® Ãâ·Â
preg_match_all("`<a href='board\.php\?.+&wr_id=.+'>(.+)</a>`iU", $text, $match);
print_r($match);
//´Ð³×ÀÓµé Å×½ºÆ® Ãâ·Â
preg_match_all("`<a href=\"javascript:;\" onClick=\"showSideView\(.+\);\".+>(.+)</a>`iU", $text, $match);
print_r($match);
?>
ÇÊ¿äÇÑ µ¥ÀÌŸ ºÎºÐ¸¸À» ¹è¿·Î °¡Á®¿ÔÀ½À» ¾Ë¼öÀÖ½À´Ï´Ù.
step4:
µ¥ÀÌŸ¸¦ °¡Á®¿À±â À§ÇØ 4¹øÀÇ Á¤±ÔÇ¥Çö½ÄÀ¸·Î ó¸®Çߴµ¥ <tr>Çϳª¿¡ ÇϳªÀÇ Á¤º¸À̹ǷÎ
Çѹ濡 ó¸®ÇÒ¼ö ÀÖ½À´Ï´Ù.
preg_match_all("`<tr align='center' height='30'><td align='left'>.*<a href='new\.php\?gr_id=.+'>(.+)</a>.*</td><td align='left'>.*<a href='new\.php\?bo_table=.+'>(.+)</a>.*</td><td.*<a href='board\.php\?.+&wr_id=.+'>(.+)</a>.*</td><td>.*<a href=\"javascript:;\" onClick=\"showSideView\(.+\);\".+>(.+)</a>.*</td>.*</tr>`iU", $text, $match);
print_r($match);
¾Æ·¡¿Í °°Àº »ùÇÃÄڵ带 ÀÛ¼ºÇÏ¿©
À¥¿¡ ¾÷·Îµå ½ÃŲÈÄ È®ÀÎÇØ º¾´Ï´Ù.
<?php
$url = 'http://sir.co.kr/bbs/new.php';
$text = file_get_contents($url);
$temp = @explode('<colgroup width="60">', $text);
$temp = @explode('</form>', $temp[1]);
$text = $temp[0];
preg_match_all("`<tr align='center' height='30'><td align='left'>.*<a href='new\.php\?gr_id=.+'>(.+)</a>.*</td><td align='left'>.*<a href='new\.php\?bo_table=.+'>(.+)</a>.*</td><td.*<a href='board\.php\?.+&wr_id=.+'>(.+)</a>.*</td><td>.*<a href=\"javascript:;\" onClick=\"showSideView\(.+\);\".+>(.+)</a>.*</td>.*</tr>`iU", $text, $match);
print_r($match);
?>
¼Ò½ºº¸±â¸¦ ÇÏ¿© ³»¿ëÀ» È®ÀÎÇϸé
$match ¹è¿ÀÇ [1] ¹ø°ºÎÅÍ ¿ì¸®°¡ ÇÊ¿äÇÑ µ¥ÀÌŸµéÀÌ ¹è¿·Î¼ Á¸ÀçÇÏ´ÂÁö È®ÀÎ ÇÒ¼ö ÀÖ½À´Ï´Ù.
step5:
µ¥ÀÌŸÀÇ °¡°ø¹× Ãâ·Â
¾Æ·¡¿Í °°Àº »ùÇÃÄڵ带 ÀÛ¼ºÇÏ¿©
À¥¿¡ ¾÷·Îµå ½ÃŲÈÄ È®ÀÎÇØ º¾´Ï´Ù.
<?php
$url = 'http://sir.co.kr/bbs/new.php';
$text = file_get_contents($url);
$temp = @explode('<colgroup width="60">', $text);
$temp = @explode('</form>', $temp[1]);
$text = $temp[0];
preg_match_all("`<tr align='center' height='30'><td align='left'>.*<a href='new\.php\?gr_id=.+'>(.+)</a>.*</td><td align='left'>.*<a href='new\.php\?bo_table=.+'>(.+)</a>.*</td><td.*<a href='board\.php\?.+&wr_id=.+'>(.+)</a>.*</td><td>.*<a href=\"javascript:;\" onClick=\"showSideView\(.+\);\".+>(.+)</a>.*</td>.*</tr>`iU", $text, $match);
if (is_array($match[1])){
$text = '<table><tr><td>±×·ì</td><td>°Ô½ÃÆÇ</td><td>Á¦¸ñ</td><td>ÀÛ¼ºÀÚ</td></tr>';
foreach($match[1] as $k => $v){
$text .= '<tr><td>' . $v . '</td><td>' . $match[2][$k] . '</td><td>' . $match[3][$k] . '</td><td>' . $match[4][$k] . '</td></tr>';
}
$text .= '</table>';
}
echo $text;
?>
ÀÌÁ¦ È®ÀÎÇØ º¸¸é ¿øÇÏ´Â µ¥ÀÌŸ´Â ¸ðµÎ Å×À̺í·Î¼ Ãâ·ÂµÊÀ» º¼¼ö ÀÖ½À´Ï´Ù.
±×·±µ¥ À̹ÌÁö °æ·ÎµîÀÌ ¸ÂÁö ¾Ê¾Æ À̹ÌÁö°¡ ±ú¾îº¸ÀÔ´Ï´Ù.
±×·¡¼ À̹ÌÁö °æ·Î¸¦ ¸ÂÃß´Â ÀÛ¾÷À» Ãß°¡ÇÕ´Ï´Ù.
¼Ò½ºº¸±â Çؼ º¸¸é
À̹ÌÁö °æ·Î°¡ ../data/member/up/uplus.gif ¿Í °°Àº ÇüÅÂÀÓÀ» º¼¼ö ÀÖ½À´Ï´Ù.
../Àº http://sir.co.kr/ À» °¡¸®Å°´Â °ÍÀ̹ǷΠÀÏ°ý ġȯÇÏ¿© ÁÝ´Ï´Ù.
$match[4][$k] = str_replace("`../`", 'http://sir.co.kr/', $match[4][$k]);
¾Æ·¡¿Í °°Àº »ùÇÃÄڵ带 ÀÛ¼ºÇÏ¿©
À¥¿¡ ¾÷·Îµå ½ÃŲÈÄ È®ÀÎÇØ º¾´Ï´Ù.
<?php
$url = 'http://sir.co.kr/bbs/new.php';
$text = file_get_contents($url);
$temp = @explode('<colgroup width="60">', $text);
$temp = @explode('</form>', $temp[1]);
$text = $temp[0];
preg_match_all("`<tr align='center' height='30'><td align='left'>.*<a href='new\.php\?gr_id=.+'>(.+)</a>.*</td><td align='left'>.*<a href='new\.php\?bo_table=.+'>(.+)</a>.*</td><td.*<a href='board\.php\?.+&wr_id=.+'>(.+)</a>.*</td><td>.*<a href=\"javascript:;\" onClick=\"showSideView\(.+\);\".+>(.+)</a>.*</td>.*</tr>`iU", $text, $match);
if (is_array($match[1])){
$text = '<table><tr><td>±×·ì</td><td>°Ô½ÃÆÇ</td><td>Á¦¸ñ</td><td>ÀÛ¼ºÀÚ</td></tr>';
foreach($match[1] as $k => $v){
$text .= '<tr><td>' . $v . '</td><td>' . $match[2][$k] . '</td><td>' . $match[3][$k] . '</td><td>' . str_replace("../", 'http://sir.co.kr/', $match[4][$k]) . '</td></tr>';
}
$text .= '</table>';
}
echo $text;
?>
°æ·Î¸¦ ¸ÂÃ߾ ¿¢¹ÚÀÌ ³ªÅ¸³ª´Â ±º¿ä
ÀÌ°ÍÀº ¼¹ö´Ü¿¡¼ ¿ÜºÎÀ̹ÌÁö ¹«´Ü ¸µÅ©¸¦ ¸·±â À§ÇØ ¼³Á¤ÇÑ °ÍÀε¥ ÀÌ°Ç ´Ù¸¥ ¹æ¹ýÀ¸·Î ÇØ°áÇØ¾ß ÇÕ´Ï´Ù.
À̳»¿ëÀÌ ¸¹Àº Ãʺ¸ ºÐµé¿¡°Ô µµ¿òÀÌ µÇ¾úÀ¸¸é ÁÁ°Ú½À´Ï´Ù.
Áß°í¼öºÐµéÀº ÇÊ¿ä¾ø´Â ³»¿ëÀϼöµµ ÀÖ°Ù½À´Ï´Ù.
|
|
̵̧ : 438 |
̵̧
¸ñ·Ï
|
|
|  |
|