如何从 Symfony 的 DomCrawler 组件获取更新的 HTML? [英] How do I get the updated HTML from Symfony's DomCrawler component?

查看:13
本文介绍了如何从 Symfony 的 DomCrawler 组件获取更新的 HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Symfony 的 DomCrawler 组件.我已经成功检索节点,添加和修改 HTML.

I'm using Symfony's DomCrawler component. I have it successfully retrieving nodes, adding and amending HTML.

但是,我不确定最终如何实际获取 HTML.我正在尝试获取被 DomCrawler 更改后的 HTML 字符串,但我不知道如何去做.

However, I'm not sure how to actually get the HTML out at the end. I'm trying to get the HTML string after it has been changed by DomCrawler, but I can't find out how to do it.

没有神奇的 __toString() 方法(当我执行 print $crawler 时它会返回错误).没有 get*() 方法,没有以 html 为值的属性.我试过 vardump($crawler) 但这没有帮助.

There's no magic __toString() method (and it returns an error when I do print $crawler). There are no get*() methods, no properties with html as a value. I've tried a vardump($crawler) but that doesn't help.

更新

如果我使用

$crawler->first()->ownerDocument->saveHtml()

它抛出一个关于在非对象上调用 saveHtml()"的异常,加上一个 PHP 错误:

it throws an exception about "calling the saveHtml() on a non-object", plus a PHP error of:

未定义的属性:Symfony\Component\DomCrawler\Crawler::$ownerDocument

Undefined property: Symfony\Component\DomCrawler\Crawler::$ownerDocument

我尝试使用 eq(0) 而不是 first() 但得到相同的错误.

I tried using eq(0) instead of first() but get the same error.

但是,如果我改为使用

each( function($node, $i) { 
  print $i . " - " . $node; }
)

然后它返回

0 - <html>...</html>

推荐答案

由于这在搜索时很早就出现了,我只是想指出一个方法 html() 已添加到爬虫中在 Symfony 2.3

As this turns up pretty early when searching, i just wanted to point out that a method html() was added to the crawler in Symfony 2.3

参见操作和倾倒爬虫"在 Symfony 文档中.

See "Manipulating and Dumping a Crawler" in the Symfony documentation.

这篇关于如何从 Symfony 的 DomCrawler 组件获取更新的 HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆