Easy Screen Scraping in PHP with the Simple HTML DOM Library
PHP를 이용해서 특정 웹문서의 HTML DOM을 손쉽게 가져올수 있는 라이브러리입니다.
온라인 document : http://simplehtmldom.sourceforge.net/manual.htm
문서는 다음과 같은 방법으로 가져옵니다.
직접 HTML string을 이용한 방법과 URL로의 접근, 그리고 local 문서로의 접근 이렇게 3가지의 방법이 있습니다.// Create a DOM object from a string
$html = str_get_html('<html><body>Hello!</body></html>');
// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');
// Create a DOM object from a HTML file
$html = file_get_html('test.htm');
그리고// Find all anchors, returns a array of element objects
$ret = $html->find('a');
// Find (N)th anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', 0);
// Find all <div> which attribute id=foo
$ret = $html->find('div[id=foo]');
// Find all <div> with the id attribute
$ret = $html->find('div[id]');
// Find all element has attribute id
$ret = $html->find('[id]');
이렇게 특정 엘리먼트를 발견할수 있고요.
엘리먼트의 속성을 가져올땐 이렇게// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
$value = $e->href;
// Set a attribute(If the attribute is non-value attribute (eg. checked, selected...), set it's value as true or false)
$e->href = 'my link';
// Remove a attribute, set it's value as null!
$e->href = null;
// Determine whether a attribute exist?
if(isset($e->href))
echo 'href exist!';
그리고 마음껏 DOM tree를 돌아다닐수도 있습니다.// If you are not so familiar with HTML DOM, check this link to learn more...
// Example
echo $html->find("#div1", 0)->children(1)->children(1)->children(2)->id;
// or
echo $html->getElementById("div1")->childNodes(1)->childNodes(1)->childNodes(2)->getAttribute('id');
참 쉽죠?
무엇보다도 이걸 이용해서
RSS 제공하지 않는 곳의 게시물을 손쉽게 비공식 적으로 RSS로 발행할수 있을것 같습니다.