使用 symfony dom 爬虫提取未标记的元素

extract untagged elements with symfony dom crawler

如何使用 symfony dom 爬虫提取未标记的元素。例如,在下面的示例 html 中,我想提取 Hello World.

<strong>title</strong>Hello World<strong>Sub-Title</strong><div>This is just stuff</div>

您可以使用 PHP DOM 轻松做到这一点 ;)

$dom = new DOMDocument();
$dom->loadHTML('<strong>title</strong>Hello World<strong>Sub-Title</strong><div>This is just stuff</div>');
$xpath = new DOMXPath($dom);
// use the fact that PHP DOM wraps everything into the body and get the text()
$entries = $xpath->query('//body/text()');
foreach ($entries as $entry) {
    echo $entry->nodeValue;
}

我有更好的方法给你

$ExtractText = $crawler->filter('strong')->eq(1)->text();

这差不多就得到了索引1的标签 因为你的 title 是索引 0