| 1 |
<?php |
| 2 |
include_once('../../simple_html_dom.php'); |
| 3 |
|
| 4 |
function scraping_slashdot() { |
| 5 |
// create HTML DOM |
| 6 |
$html = file_get_html('http://slashdot.org/'); |
| 7 |
|
| 8 |
// get article block |
| 9 |
foreach($html->find('div[id^=firehose-]') as $article) { |
| 10 |
// get title |
| 11 |
$item['title'] = trim($article->find('a.datitle', 0)->plaintext); |
| 12 |
// get body |
| 13 |
$item['body'] = trim($article->find('div.body', 0)->plaintext); |
| 14 |
|
| 15 |
$ret[] = $item; |
| 16 |
} |
| 17 |
|
| 18 |
// clean up memory |
| 19 |
$html->clear(); |
| 20 |
unset($html); |
| 21 |
|
| 22 |
return $ret; |
| 23 |
} |
| 24 |
|
| 25 |
// ----------------------------------------------------------------------------- |
| 26 |
// test it! |
| 27 |
$ret = scraping_slashdot(); |
| 28 |
|
| 29 |
foreach($ret as $v) { |
| 30 |
echo $v['title'].'<br>'; |
| 31 |
echo '<ul>'; |
| 32 |
echo '<li>'.$v['body'].'</li>'; |
| 33 |
echo '</ul>'; |
| 34 |
} |
| 35 |
?> |