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

· 9년 전 · 645


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년 전 조회 465
9년 전 조회 529
9년 전 조회 529
9년 전 조회 672
9년 전 조회 487
9년 전 조회 624
9년 전 조회 453
9년 전 조회 503
9년 전 조회 390
9년 전 조회 590
9년 전 조회 480
9년 전 조회 487
9년 전 조회 528
9년 전 조회 489
9년 전 조회 490
9년 전 조회 452
9년 전 조회 835
9년 전 조회 877
9년 전 조회 854
9년 전 조회 781
9년 전 조회 569
9년 전 조회 747
9년 전 조회 868
9년 전 조회 553
9년 전 조회 846
9년 전 조회 812
9년 전 조회 1,206
9년 전 조회 820
9년 전 조회 1,016
9년 전 조회 671
9년 전 조회 804
9년 전 조회 565
9년 전 조회 542
9년 전 조회 672
9년 전 조회 637
9년 전 조회 823
9년 전 조회 533
9년 전 조회 616
9년 전 조회 567
9년 전 조회 507
9년 전 조회 583
9년 전 조회 451
9년 전 조회 484
9년 전 조회 442
9년 전 조회 507
9년 전 조회 487
9년 전 조회 925
9년 전 조회 456
9년 전 조회 386
9년 전 조회 646
9년 전 조회 469
9년 전 조회 473
9년 전 조회 435
9년 전 조회 400
9년 전 조회 362
9년 전 조회 339
9년 전 조회 394
9년 전 조회 414
9년 전 조회 362
9년 전 조회 302
9년 전 조회 409
9년 전 조회 354
9년 전 조회 378
9년 전 조회 402
9년 전 조회 439
9년 전 조회 323
9년 전 조회 367
9년 전 조회 425
9년 전 조회 355
9년 전 조회 471
9년 전 조회 397
9년 전 조회 338
9년 전 조회 306
9년 전 조회 352
9년 전 조회 307
9년 전 조회 339
9년 전 조회 369
9년 전 조회 300
9년 전 조회 355
9년 전 조회 317
9년 전 조회 423
9년 전 조회 514
10년 전 조회 468
10년 전 조회 418
10년 전 조회 368
10년 전 조회 440
10년 전 조회 373
10년 전 조회 398
10년 전 조회 350
10년 전 조회 318
10년 전 조회 319
10년 전 조회 350
10년 전 조회 421
10년 전 조회 383
10년 전 조회 437
10년 전 조회 368
10년 전 조회 366
10년 전 조회 364
10년 전 조회 406
10년 전 조회 490
🐛 버그신고