메뉴 건너뛰기

프로그램언어

조회 수 225 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

http://simplehtmldom.sourceforge.net/

 

https://github.com/samacs/simple_html_dom

 

다운로드 링크 : http://sourceforge.net/projects/simplehtmldom/files/latest/download?source=files

 

해당 홈에 나와있는 예제입니다.

 

간단한 주석을 붙입니다.

 

//html 을 가져오고

$html = file_get_html('http://www.google.com/');            



// 모든 이미지태그를 찾아냅니다.

foreach($html->find('img') as $element) 

       echo $element->src . '<br>';                                  



// 모든 a태그를 찾아내어 href속성을 뿌려줍니다.

foreach($html->find('a') as $element)                             

       echo $element->href . '<br>';



// 컨텐츠내에 텍스트들만 가져옵니다.

echo file_get_html('http://www.google.com/')->plaintext; 



// css jquery 많이 만져보셨다면 선택자는 거의 동일합니다.

//몇번째 a태그 같은 경우도 가져올수 있구요

foreach($html->find('div.article') as $article) {

    $item['title']     = $article->find('div.title', 0)->plaintext;

    $item['intro']    = $article->find('div.intro', 0)->plaintext;

    $item['details'] = $article->find('div.details', 0)->plaintext;

    $articles[] = $item;

}



//가져오기전 해당 태그내의 텍스트도 이렇게 간단히 변경가능합니다.

$html->find('div[id=hello]', 0)->innertext = 'foo';



예제

<?php

include_once('simplehtmldom_1_5/simple_html_dom.php');

// 네이버 html을 가져온다

$html = file_get_html('http://www.naver.com/');



// 모든 이미지태그를 찾고

foreach($html->find('img') as $element) {

       echo $element->src . '<br>';

}       

 

// 모든 a태그를 찾아내어 href속성을 뿌려줍니다.

foreach($html->find('a') as $element) {                            

       echo $element->href . '<br>';

}    

?>

 


  1. PHP error 메시지 출력

    Date2015.04.14 Views20929
    Read More
  2. PHP EXCEL export시 시트 이름 지정하여 여러 시트에 데이터 쓰기

    Date2017.03.06 Views18275
    Read More
  3. PHP http -> https 로 전환

    Date2023.01.12 Views240
    Read More
  4. PHP http 를 https 로 전환(redirect), http->https

    Date2019.02.19 Views2187
    Read More
  5. PHP Mcrypt 라이브러리를 활용한 암호화 시스템

    Date2016.12.22 Views19981
    Read More
  6. PHP Notice: Use of undefined constant ... assumed ... 오류

    Date2021.03.26 Views310
    Read More
  7. PHP SimpleHtmlDom Parser로 HTML 파싱하기

    Date2023.01.12 Views225
    Read More
  8. PHP split()와 explode()의 차이점

    Date2018.10.27 Views3542
    Read More
  9. PHP str_replace php 문자열치환

    Date2023.01.12 Views202
    Read More
  10. PHP switch 문

    Date2015.04.14 Views20996
    Read More
  11. php www 붙이기

    Date2023.01.12 Views237
    Read More
  12. PHP XML 문서파싱 (SAX 방식 , DOM 방식)

    Date2018.10.27 Views3589
    Read More
  13. PHP ZIP 압축파일 만들기

    Date2023.01.12 Views256
    Read More
  14. PHP 가변변수 $$

    Date2019.02.19 Views1454
    Read More
  15. php 간단 심플한 달력만들기

    Date2023.01.12 Views245
    Read More
  16. PHP 날짜 함수

    Date2017.04.13 Views17687
    Read More
  17. PHP 날짜, 시간 관련 함수. date(), mktime()

    Date2017.03.06 Views16712
    Read More
  18. PHP 날짜/시간 정리

    Date2017.03.07 Views18636
    Read More
  19. php 내장함수

    Date2017.03.07 Views17018
    Read More
  20. PHP 네이버블로그 원격 글쓰기 API 소스

    Date2018.02.09 Views12481
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved