d.g*_*zha 3 php symfony goutte
html结构:
<div id="product">
<p>some text</p>
<p>some text2</p>
</div>
Run Code Online (Sandbox Code Playgroud)
我的PHP代码:
$client = new Client();
$crawler = $client->request('GET', $url);
echo $crawler->filter('#product')->text();
Run Code Online (Sandbox Code Playgroud)
返回:
some text some text2
Run Code Online (Sandbox Code Playgroud)
但是我需要:
<p>some text</p>
<p>some text2</p>
Run Code Online (Sandbox Code Playgroud)
好吧,有一种但丑陋的方法 - 通过迭代其节点:
$html = '';
foreach ($crawler as $domElement) {
$html.= $domElement->ownerDocument->saveHTML();
}
Run Code Online (Sandbox Code Playgroud)
或者,在您的情况下,您应该迭代过滤的元素:
$html = '';
$product = $crawler->filter('#produkt');
foreach ($product as $domElement) {
foreach($domElement->childNodes as $node) {
$html .= $domElement->ownerDocument->saveHTML($node);
}
}
Run Code Online (Sandbox Code Playgroud)
从文档
| 归档时间: |
|
| 查看次数: |
3658 次 |
| 最近记录: |