Skip to content

Instantly share code, notes, and snippets.

@hugmouse
Last active April 24, 2019 07:03
Show Gist options
  • Save hugmouse/fdeddbad93a04f4da90d240fa3d83707 to your computer and use it in GitHub Desktop.
Save hugmouse/fdeddbad93a04f4da90d240fa3d83707 to your computer and use it in GitHub Desktop.
Простой пример работы с Simple Html Dom - переход по ссылкам, найденым на изначальной странице
<?php
include "./simple_html_dom.php";
// Создаем дом
$html = file_get_html('https://bash.im');
// Находим ссылки
foreach($html->find('a[class=quote__header_permalink]') as $a) {
// Находим ссылки и берем у них аттрибут "Href", он бывает относительный и поэтому...
$url = $a->href;
// ...я добавляю "https://bash.im" здесь, так как на этом сайте все ссылки - относительные.
$second = file_get_html("https://bash.im".$url);
// Здесь просто форматирование для логов
echo "========================" . PHP_EOL . "Visiting: https://bash.im" . $url . PHP_EOL . "========================" . PHP_EOL;
// На каждой страничке находим div с классом "quote__body" и выводим его внутри
foreach ($second->find('div[class=quote__body]') as $body) {
echo $body->plaintext . PHP_EOL . "========================" . PHP_EOL . PHP_EOL;
sleep(2);
}
// После каждого запроса и парсинга страницы - чистим память
$second->clear();
unset($second);
}
?>
@hugmouse
Copy link
Author

Example output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment