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

· 9년 전 · 745


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년 전 조회 566
9년 전 조회 626
9년 전 조회 625
9년 전 조회 760
9년 전 조회 583
9년 전 조회 701
9년 전 조회 542
9년 전 조회 603
9년 전 조회 483
9년 전 조회 674
9년 전 조회 563
9년 전 조회 575
9년 전 조회 619
9년 전 조회 577
9년 전 조회 582
9년 전 조회 540
9년 전 조회 920
9년 전 조회 972
9년 전 조회 949
9년 전 조회 878
9년 전 조회 667
9년 전 조회 846
9년 전 조회 958
9년 전 조회 646
9년 전 조회 946
9년 전 조회 906
9년 전 조회 1,307
9년 전 조회 921
9년 전 조회 1,108
9년 전 조회 771
9년 전 조회 895
9년 전 조회 656
9년 전 조회 640
9년 전 조회 762
9년 전 조회 729
9년 전 조회 899
9년 전 조회 631
9년 전 조회 706
9년 전 조회 655
9년 전 조회 592
9년 전 조회 681
9년 전 조회 546
9년 전 조회 583
9년 전 조회 531
9년 전 조회 598
9년 전 조회 584
9년 전 조회 1,033
9년 전 조회 554
9년 전 조회 484
9년 전 조회 746
9년 전 조회 567
9년 전 조회 559
9년 전 조회 525
9년 전 조회 503
9년 전 조회 451
9년 전 조회 425
9년 전 조회 506
9년 전 조회 521
9년 전 조회 460
9년 전 조회 405
9년 전 조회 511
9년 전 조회 451
9년 전 조회 480
9년 전 조회 519
9년 전 조회 540
9년 전 조회 434
9년 전 조회 475
9년 전 조회 536
9년 전 조회 470
9년 전 조회 574
9년 전 조회 511
9년 전 조회 448
10년 전 조회 410
10년 전 조회 459
10년 전 조회 399
10년 전 조회 444
10년 전 조회 474
10년 전 조회 406
10년 전 조회 460
10년 전 조회 420
10년 전 조회 522
10년 전 조회 623
10년 전 조회 580
10년 전 조회 534
10년 전 조회 465
10년 전 조회 550
10년 전 조회 470
10년 전 조회 504
10년 전 조회 459
10년 전 조회 433
10년 전 조회 437
10년 전 조회 463
10년 전 조회 534
10년 전 조회 486
10년 전 조회 548
10년 전 조회 467
10년 전 조회 480
10년 전 조회 476
10년 전 조회 511
10년 전 조회 602
🐛 버그신고