php domcrawler,php – Goutte – dom crawler – 删除节点

本文介绍了如何使用Symfony的DomCrawler组件从HTML文档中导航并提取数据,强调该组件设计用于读取而非修改DOM。示例代码展示了如何删除特定节点。尽管不鼓励直接操作DOM,但通过引用传递的对象特性使得在技术上可以实现。
摘要由CSDN通过智能技术生成

click

back

click

back

我想收到:

$client = new Client();

$crawler = $client->request('GET', 'http://testsite.com/test.php');

$crawler->filter('.first .second')->each(function ($node) {

//??????

});

解决方法:

The DomCrawler component eases DOM navigation for HTML and XML documents.

并且:

While possible, the DomCrawler component is not designed for manipulation of the DOM or re-dumping HTML/XML.

DomCrawler旨在从DOM文档中提取细节而不是修改它们.

然而…

由于PHP通过引用传递对象,而Crawler基本上是DOMNodes的包装器,因此在技术上可以修改底层DOM文档:

// will remove all span nodes inside .second nodes

$crawler->filter('html .content h2')->each(function (Crawler $crawler) {

foreach ($crawler as $node) {

$node->parentNode->removeChild($node);

}

});

标签:php,dom,web-crawler,symfony,goutte

来源: https://codeday.me/bug/20190628/1319962.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值