| 1 |
<?php |
| 2 |
include_once('../../simple_html_dom.php'); |
| 3 |
|
| 4 |
function scraping_digg() { |
| 5 |
// create HTML DOM |
| 6 |
$html = file_get_html('http://digg.com/'); |
| 7 |
|
| 8 |
// get news block |
| 9 |
foreach($html->find('div.news-summary') as $article) { |
| 10 |
// get title |
| 11 |
$item['title'] = trim($article->find('h3', 0)->plaintext); |
| 12 |
// get details |
| 13 |
$item['details'] = trim($article->find('p', 0)->plaintext); |
| 14 |
// get intro |
| 15 |
$item['diggs'] = trim($article->find('li a strong', 0)->plaintext); |
| 16 |
|
| 17 |
$ret[] = $item; |
| 18 |
} |
| 19 |
|
| 20 |
// clean up memory |
| 21 |
$html->clear(); |
| 22 |
unset($html); |
| 23 |
|
| 24 |
return $ret; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
// ----------------------------------------------------------------------------- |
| 29 |
// test it! |
| 30 |
|
| 31 |
// "http://digg.com" will check user_agent header... |
| 32 |
ini_set('user_agent', 'My-Application/2.5'); |
| 33 |
|
| 34 |
$ret = scraping_digg(); |
| 35 |
|
| 36 |
foreach($ret as $v) { |
| 37 |
echo $v['title'].'<br>'; |
| 38 |
echo '<ul>'; |
| 39 |
echo '<li>'.$v['details'].'</li>'; |
| 40 |
echo '<li>Diggs: '.$v['diggs'].'</li>'; |
| 41 |
echo '</ul>'; |
| 42 |
} |
| 43 |
|
| 44 |
?> |