소개
PHP로 명령어 기반의 프로그램을 만들 수 있다는 것을 아시나요? PHP로 웹페이지를 생산하는 것 뿐아니라, 웹페이지를 다운로드 받아서 분석하는 프로그램을 만들 수 있다는 것은 아시나요? 여기서는 PHP를 간단하게 사용해서 웹페이지를 분석해서 필요한 정보만을 출력해주는 프로그램을 만드는 방법을 알아봅니다.
수업
소스 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php include 'simple_html_dom.php' ; $url = $argv [1]; $html = file_get_html( $url ); $output = '' ; for ( $i =2; $i < count ( $argv ); $i ++){ if ( substr ( $argv [ $i ], 0, 2) === '-t' ) { $output .= substr ( $argv [ $i ], 2); } else { $output .= trim( $html ->find( $argv [ $i ])[0]->plaintext); } } echo $output ; |
사용방법
php check.php 주소 출력할 내용
예제
- 추출할 데이터가 있는 웹사이트의 주소 : https://onoffmix.com/event/108060
- 제목이 담겨 있는 태그의 선택자 : 'title'
- 남은 자리가 담겨 있는 태그의 선택자 : '.attend .free .number'
- 전체 자리가 담겨 있는 태그의 선택자 : '.attend .total .number'
- 원하는 형식 : 제목 : 참가가능/정원
- 실행 : php check.php https://onoffmix.com/event/108060 'title' -t' : ' '.attend .free .number' -t'/' '.attend .total .number'
- 결과 : 온오프믹스 :: 코딩야학 경기도 방문수업 : 6/150
참고
- 이 앱을 만드는 과정 : https://opentutorials.org/module/2503/17115