2

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

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

2 回答 2

1

我有更好的方法给你

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

这几乎得到了索引 1 的标签,因为您的标题是索引 0

于 2019-09-26T07:43:46.500 回答
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;
}
于 2015-07-02T12:46:25.000 回答