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

· 9년 전 · 677


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;

?>

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

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

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

프로그램

태그 필터 (최대 3개) 전체 개발자 소스 기타 mysql 팁자료실 javascript php linux flash 정규표현식 jquery node.js mobile 웹서버 os 프로그램 강좌 썸네일 이미지관련 도로명주소 그누보드5 기획자 견적서 계약서 기획서 마케팅 제안서 seo 통계 서식 통계자료 퍼블리셔 html css 반응형 웹접근성 퍼블리싱 표준화 반응형웹 홈페이지기초 부트스트랩 angularjs 포럼 스크린리더 센스리더 개발자톡 개발자팁 퍼블리셔톡 퍼블리셔팁 기획자톡 기획자팁 프로그램강좌 퍼블리싱강좌
+
제목 글쓴이 날짜 조회
9년 전 조회 500
9년 전 조회 560
9년 전 조회 553
9년 전 조회 696
9년 전 조회 521
9년 전 조회 645
9년 전 조회 474
9년 전 조회 538
9년 전 조회 416
9년 전 조회 618
9년 전 조회 501
9년 전 조회 507
9년 전 조회 556
9년 전 조회 516
9년 전 조회 518
9년 전 조회 482
9년 전 조회 868
9년 전 조회 903
9년 전 조회 886
9년 전 조회 812
9년 전 조회 603
9년 전 조회 773
9년 전 조회 897
9년 전 조회 588
9년 전 조회 890
9년 전 조회 843
9년 전 조회 1,240
9년 전 조회 856
9년 전 조회 1,046
9년 전 조회 695
9년 전 조회 834
9년 전 조회 589
9년 전 조회 577
9년 전 조회 701
9년 전 조회 667
9년 전 조회 849
9년 전 조회 564
9년 전 조회 647
9년 전 조회 596
9년 전 조회 529
9년 전 조회 619
9년 전 조회 478
9년 전 조회 516
9년 전 조회 468
9년 전 조회 534
9년 전 조회 517
9년 전 조회 968
9년 전 조회 491
9년 전 조회 420
9년 전 조회 678
9년 전 조회 497
9년 전 조회 495
9년 전 조회 468
9년 전 조회 434
9년 전 조회 386
9년 전 조회 363
9년 전 조회 435
9년 전 조회 448
9년 전 조회 398
9년 전 조회 340
9년 전 조회 436
9년 전 조회 390
9년 전 조회 416
9년 전 조회 446
9년 전 조회 472
9년 전 조회 357
9년 전 조회 408
9년 전 조회 466
9년 전 조회 397
9년 전 조회 512
9년 전 조회 444
9년 전 조회 377
9년 전 조회 342
9년 전 조회 395
9년 전 조회 342
9년 전 조회 377
9년 전 조회 404
9년 전 조회 336
9년 전 조회 387
9년 전 조회 355
9년 전 조회 452
9년 전 조회 554
10년 전 조회 511
10년 전 조회 462
10년 전 조회 390
10년 전 조회 475
10년 전 조회 408
10년 전 조회 435
10년 전 조회 384
10년 전 조회 359
10년 전 조회 356
10년 전 조회 387
10년 전 조회 458
10년 전 조회 419
10년 전 조회 473
10년 전 조회 398
10년 전 조회 400
10년 전 조회 405
10년 전 조회 445
10년 전 조회 529
🐛 버그신고