Last active
July 16, 2020 00:06
-
-
Save spacebit-official/00378b656ccc164dc140a35b10bccdfb to your computer and use it in GitHub Desktop.
phpQuery
This file contains hidden or 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
| <? | |
| /////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| $url = 'http://www.kolesa.ru/news/page/1'; | |
| $title = pq('title')->html();// получения по селектору | |
| $article->find('.class:eq(3)')->text(); // 4 по счету класс | |
| $article->find('.cat')->remove(); //удаляем нужный блок с классом | |
| $article->find('.cat')->prepend('Категория: '); //добавляем текст в нужный блок с классом | |
| $article->find('.cat')->wrap('<div class=category>')->after('Дата:' . date('Y-M-D')); //делаем обертку и добавляемпосле класса .cat дату | |
| phpQuery::unloadDocuments($doc); //чистим память | |
| $article->find('div.forecast-briefly__day:not(.forecast-briefly__day_weekstart_0)'); //кроме | |
| /////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
| function parser($url, $start, $end) { | |
| if ($start < $end) { | |
| $file = file_get_contents($url); | |
| $doc = phpQuery::newDocument($file); | |
| foreach ($doc->find('.articles-container .post-excerpt') as $article) { | |
| $article = pq($article); | |
| $img = $article->find('.img-cont img')->attr('src'); | |
| $text = $article->find('.pd-cont')->html(); //берем весь html код | |
| echo "<img src='$img'>"; | |
| echo $text; | |
| echo "<hr>"; | |
| } | |
| $next = $doc->find('.pages-nav .current')->next()->attr('href'); | |
| if (!empty($next)) { | |
| $start++; | |
| parser($next, $start, $end); | |
| } | |
| } | |
| phpQuery::unloadDocuments($doc); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Вопрос - однотипный код, ни единого класса ни единого айди. Но есть однотипные "поля" - т.е. можно прицепиться только к тексту. Что делать ?
т.е. - ожидается команда типа: $document->find(div > '#container')->text('Дата: ');
на выходе должны получить чтото типа вот этого: "
Дата: 24-05-2019
"Исходный код страницы:
<div>
<div><p><strong>Дата: </strong>24-05-2019</p></div>
<div><p><strong>Учасник: </strong>Вася</p></div>
<div><p><strong>Оценка: </strong>5</p></div>
</div>
<div>
<div><p><strong>Дата: </strong>24-05-2019</p></div>
<div><p><strong>Учасник: </strong>Петя</p></div>
<div><p><strong>Примечание: </strong>Балбес</p></div>
</div>
...