| 1 |
<?php |
| 2 |
include_once('../../simple_html_dom.php'); |
| 3 |
|
| 4 |
function scraping_IMDB($url) { |
| 5 |
// create HTML DOM |
| 6 |
$html = file_get_html($url); |
| 7 |
|
| 8 |
// get title |
| 9 |
$ret['Title'] = $html->find('title', 0)->innertext; |
| 10 |
|
| 11 |
// get rating |
| 12 |
$ret['Rating'] = $html->find('div[class="general rating"] b', 0)->innertext; |
| 13 |
|
| 14 |
// get overview |
| 15 |
foreach($html->find('div[class="info"]') as $div) { |
| 16 |
// skip user comments |
| 17 |
if($div->find('h5', 0)->innertext=='User Comments:') |
| 18 |
return $ret; |
| 19 |
|
| 20 |
$key = ''; |
| 21 |
$val = ''; |
| 22 |
|
| 23 |
foreach($div->find('*') as $node) { |
| 24 |
if ($node->tag=='h5') |
| 25 |
$key = $node->plaintext; |
| 26 |
|
| 27 |
if ($node->tag=='a' && $node->plaintext!='more') |
| 28 |
$val .= trim(str_replace("\n", '', $node->plaintext)); |
| 29 |
|
| 30 |
if ($node->tag=='text') |
| 31 |
$val .= trim(str_replace("\n", '', $node->plaintext)); |
| 32 |
} |
| 33 |
|
| 34 |
$ret[$key] = $val; |
| 35 |
} |
| 36 |
|
| 37 |
// clean up memory |
| 38 |
$html->clear(); |
| 39 |
unset($html); |
| 40 |
|
| 41 |
return $ret; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
// ----------------------------------------------------------------------------- |
| 46 |
// test it! |
| 47 |
$ret = scraping_IMDB('http://imdb.com/title/tt0335266/'); |
| 48 |
|
| 49 |
foreach($ret as $k=>$v) |
| 50 |
echo '<strong>'.$k.' </strong>'.$v.'<br>'; |
| 51 |
?> |