如何使用 symfony dom 爬虫提取未标记的元素。例如,在下面的示例 html 中,我想提取Hello World
.
<strong>title</strong>Hello World<strong>Sub-Title</strong><div>This is just stuff</div>
如何使用 symfony dom 爬虫提取未标记的元素。例如,在下面的示例 html 中,我想提取Hello World
.
<strong>title</strong>Hello World<strong>Sub-Title</strong><div>This is just stuff</div>
我有更好的方法给你
$ExtractText = $crawler->filter('strong')->eq(1)->text();
这几乎得到了索引 1 的标签,因为您的标题是索引 0
您可以使用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;
}