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

· 9년 전 · 577


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년 전 조회 403
9년 전 조회 459
9년 전 조회 465
9년 전 조회 609
9년 전 조회 420
9년 전 조회 540
9년 전 조회 389
9년 전 조회 429
9년 전 조회 344
9년 전 조회 531
9년 전 조회 417
9년 전 조회 422
9년 전 조회 462
9년 전 조회 421
9년 전 조회 415
9년 전 조회 377
9년 전 조회 762
9년 전 조회 822
9년 전 조회 785
9년 전 조회 710
9년 전 조회 493
9년 전 조회 680
9년 전 조회 789
9년 전 조회 483
9년 전 조회 774
9년 전 조회 736
9년 전 조회 1,137
9년 전 조회 744
9년 전 조회 941
9년 전 조회 620
9년 전 조회 725
9년 전 조회 511
9년 전 조회 469
9년 전 조회 597
9년 전 조회 566
9년 전 조회 748
9년 전 조회 457
9년 전 조회 542
9년 전 조회 497
9년 전 조회 437
9년 전 조회 511
9년 전 조회 394
9년 전 조회 413
9년 전 조회 378
9년 전 조회 424
9년 전 조회 412
9년 전 조회 862
9년 전 조회 380
9년 전 조회 313
9년 전 조회 578
9년 전 조회 385
9년 전 조회 412
9년 전 조회 364
9년 전 조회 338
9년 전 조회 296
9년 전 조회 282
9년 전 조회 337
9년 전 조회 358
9년 전 조회 296
9년 전 조회 240
9년 전 조회 350
9년 전 조회 290
9년 전 조회 318
9년 전 조회 330
9년 전 조회 374
9년 전 조회 263
9년 전 조회 304
9년 전 조회 369
9년 전 조회 289
9년 전 조회 406
9년 전 조회 341
9년 전 조회 274
9년 전 조회 244
9년 전 조회 293
9년 전 조회 238
9년 전 조회 278
9년 전 조회 300
9년 전 조회 241
9년 전 조회 296
9년 전 조회 255
9년 전 조회 368
9년 전 조회 456
10년 전 조회 413
10년 전 조회 364
10년 전 조회 321
10년 전 조회 369
10년 전 조회 310
10년 전 조회 340
10년 전 조회 293
10년 전 조회 262
10년 전 조회 271
10년 전 조회 304
10년 전 조회 362
10년 전 조회 329
10년 전 조회 382
10년 전 조회 303
10년 전 조회 303
10년 전 조회 305
10년 전 조회 337
10년 전 조회 426
🐛 버그신고