그누보드 최근 게시물 파싱하기 입니다.

· 9년 전 · 588


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;

?>

경로를 맞추어도 엑박이 나타나는 군요

이것은 서버단에서 외부이미지 무단 링크를 막기 위해 설정한 것인데 이건 다른 방법으로 해결해야 합니다.
 

|
댓글을 작성하시려면 로그인이 필요합니다. 로그인

프로그램

+
제목 글쓴이 날짜 조회
9년 전 조회 414
9년 전 조회 471
9년 전 조회 475
9년 전 조회 618
9년 전 조회 428
9년 전 조회 555
9년 전 조회 397
9년 전 조회 439
9년 전 조회 347
9년 전 조회 542
9년 전 조회 422
9년 전 조회 435
9년 전 조회 473
9년 전 조회 431
9년 전 조회 427
9년 전 조회 389
9년 전 조회 775
9년 전 조회 828
9년 전 조회 794
9년 전 조회 719
9년 전 조회 504
9년 전 조회 688
9년 전 조회 795
9년 전 조회 494
9년 전 조회 784
9년 전 조회 746
9년 전 조회 1,147
9년 전 조회 751
9년 전 조회 953
9년 전 조회 625
9년 전 조회 735
9년 전 조회 515
9년 전 조회 479
9년 전 조회 605
9년 전 조회 575
9년 전 조회 758
9년 전 조회 469
9년 전 조회 550
9년 전 조회 503
9년 전 조회 442
9년 전 조회 517
9년 전 조회 396
9년 전 조회 425
9년 전 조회 388
9년 전 조회 432
9년 전 조회 422
9년 전 조회 870
9년 전 조회 387
9년 전 조회 325
9년 전 조회 588
9년 전 조회 395
9년 전 조회 416
9년 전 조회 368
9년 전 조회 345
9년 전 조회 298
9년 전 조회 286
9년 전 조회 342
9년 전 조회 362
9년 전 조회 299
9년 전 조회 244
9년 전 조회 354
9년 전 조회 295
9년 전 조회 321
9년 전 조회 334
9년 전 조회 375
9년 전 조회 264
9년 전 조회 306
9년 전 조회 370
9년 전 조회 291
9년 전 조회 408
9년 전 조회 342
9년 전 조회 276
9년 전 조회 245
9년 전 조회 294
9년 전 조회 239
9년 전 조회 280
9년 전 조회 302
9년 전 조회 242
9년 전 조회 297
9년 전 조회 256
9년 전 조회 369
9년 전 조회 457
10년 전 조회 415
10년 전 조회 364
10년 전 조회 323
10년 전 조회 370
10년 전 조회 312
10년 전 조회 341
10년 전 조회 293
10년 전 조회 263
10년 전 조회 271
10년 전 조회 304
10년 전 조회 362
10년 전 조회 329
10년 전 조회 382
10년 전 조회 305
10년 전 조회 303
10년 전 조회 306
10년 전 조회 338
10년 전 조회 428
🐛 버그신고