Saturday, April 11, 2015

[PHP] Simple HTML Parser

11 April 2015
Inspect Element

Intro::


Simple HTML parser, pake Simple DOM HTML Parser.

Steps::

Download Script DOM Html Parsser
1. http://simplehtmldom.sourceforge.net/
2. install & extract localhost/twitplay/lib/dom/

Simple Parsing html
1. copas script dibawah ini untuk parsing Berita Terbaru dari official jkt48

<meta charset="UTF-8">
<?php include "lib/dom/simple_html_dom.php"; 

// Create DOM from URL or file
$url = 'http://jkt48.com/news/list?lang=id';
$html = file_get_html($url);

//set counter
$c = 1;

// Find all post
foreach($html->find('div[class=post]') as $element){
foreach($element->find('div[class=contentpink]') as $element2){
// $c as counter
echo $c.'. ';
echo $element2->find('div[class=excerpt]')[0]->plaintext;
echo ' ';
echo $element2->find('div[class=metadata]')[0]->innertext;
echo '<br><br>';
$c++;
}
}

?>

2. simpan beritajkt.php dan panggil di browser

Catatan::

Simple html dom parser manual : http://simplehtmldom.sourceforge.net/manual.htm

No comments:

Post a Comment