Closed duncanmcclean closed 5 years ago
$node->parentNode->removeChild($node);
will do it :)
Hopefully it makes ReadCast an even better software :)
Do I put that inside a filter @NinoSkopac?
No, in the callback closure.
On Sun, 21 Apr 2019 at 08:20, Duncan McClean notifications@github.com wrote:
Do I put that inside a filter @NinoSkopac https://github.com/NinoSkopac?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/FriendsOfPHP/Goutte/issues/377#issuecomment-485230976, or mute the thread https://github.com/notifications/unsubscribe-auth/AANTPNAUBA22HGDNA7VMNBTPRQITDANCNFSM4HEQMKOA .
Sorry, I don't understand. Do you mind providing an example?
I have just attempted to do this using this code:
$crawler
->filter('nav, header, #nav, #header')
->each(function ($node) use (&$body) {
$node->parentNode->removeChild($node);
});
With that I get the error message, Undefined property: Symfony\Component\DomCrawler\Crawler::$parentNode
the argument of the each
method is a Crawler object, not a DomElement node. You need to get the node out of it.
Exactly, typehint it like this: function (Crawler $node) { ...calback code... }
And you can get the node out of it thus: $node->getNode(0);
Thanks for your help! Managed to achieve it!
$crawler
->filter('head, script, nav, header, #nav, #header, .navbar, #navbar, footer, .footer #foorer, .sidebar, #sidebar, .comments, #comments, .pagination, button')
->each(function (Crawler $crawler) {
foreach ($crawler as $node) {
$node->parentNode->removeChild($node);
}
});
Hi,
Is there any way using Goutte to remove nodes? I'm building a parser but I need to remove header and footer elements, along with others.
I've briefly looked at the source code for Symfony Crawler and I found the
reduce
function but I'm unsure on how to use it.