HTML 结构:
<div id="product">
<p>some text</p>
<p>some text2</p>
</div>
我的 PHP 代码:
$client = new Client();
$crawler = $client->request('GET', $url);
echo $crawler->filter('#product')->text();
返回:
some text some text2
但我需要:
<p>some text</p>
<p>some text2</p>
最佳答案
好吧,有一种方法很丑陋——遍历它的节点:
$html = '';
foreach ($crawler as $domElement) {
$html.= $domElement->ownerDocument->saveHTML();
}
或者,在您的情况下,您应该遍历过滤后的元素:
$html = '';
$product = $crawler->filter('#produkt');
foreach ($product as $domElement) {
foreach($domElement->childNodes as $node) {
$html .= $domElement->ownerDocument->saveHTML($node);
}
}
关于php - 使用 Symfony DomCrawler 获取元素的原始 HTML 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15133541/