Last active
April 24, 2019 07:03
-
-
Save hugmouse/fdeddbad93a04f4da90d240fa3d83707 to your computer and use it in GitHub Desktop.
Простой пример работы с Simple Html Dom - переход по ссылкам, найденым на изначальной странице
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
?> |
Author
hugmouse
commented
Apr 23, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment