如何使用this solution here实现Domcrawler?
<?php
use Symfony\Component\DomCrawler\Crawler;
$crawler = new Crawler();
$content = file_get_contents('http://example.com/somepage.html');
$crawler->addHtmlContent($content, 'UTF-8');
$content = $crawler->filter('#main-content');
// Remove content by tag and by css selector.
?>
最佳答案
$crawler = new Crawler($html,$url);
$document = new \DOMDocument('1.0', 'UTF-8');
$root = $document->appendChild($document->createElement('_root'));
$crawler->rewind();
$root->appendChild($document->importNode($crawler->current(), true));
$domxpath = new \DOMXPath($document);
foreach ($selectorsToRemove as $selector) {
$crawlerInverse = $domxpath->query(CssSelector::toXPath($selector));
foreach ($crawlerInverse as $elementToRemove) {
$parent = $elementToRemove->parentNode;
$parent->removeChild($elementToRemove);
}
}
$crawler->clear();
$crawler->add($document);
关于dom - 如何使用Domcrawler通过CSS选择器(标签,类和ID)删除元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17070100/