[PHP] PHP로 HTML DOM 파싱하기
- Download
- simple_html_dom.php(53.6 KB) 2017-08-250
- Link
- http://simplehtmldom.sourceforge.net/manual.htm16
예제1 - How to get HTML elements?
// php simple html dom 호출
include ("simplehtmldom.php");
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
예제2 - How to modify HTML elements?
// php simple html dom 호출
include ("simplehtmldom.php");
// Create DOM from string
$html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');
$html->find('div', 1)->class = 'bar';
$html->find('div[id=hello]', 0)->innertext = 'foo';
echo $html; // Output: <div id="hello">foo</div><div id="world" class="bar">World</div>
예제3 - Extract contents from HTML?
// php simple html dom 호출
include ("simplehtmldom.php");
// Dump contents (without tags) from HTML
echo file_get_html('http://www.google.com/')->plaintext;
예제4 - Scraping Slashdot!
// php simple html dom 호출
include ("simplehtmldom.php");
// Create DOM from URL
$html = file_get_html('http://slashdot.org/');
// Find all article blocks
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;
}
print_r($articles);