毛刺在PHP简单的HTML DOM解析器
问题描述:
我使用PHP简单的HTML DOM解析器刮网上商店(也运行XAMPP 1.7.2与PHP5.3.0)的一些数据,和我正在与<tbody>
标签问题。该表的结构,essentialy(细节并不真的那么重要):<tbody>毛刺在PHP简单的HTML DOM解析器
<table>
<thead>
<!--text here-->
</thead>
<tbody>
<!--text here-->
</tbody>
</table>
现在,我尝试使用代码来获取到<tbody>
部分:
$element = $html->find('tbody',0)->innertext;
一点也没有不会抛出任何错误,当我试图回显它时,它不会打印任何内容。我已经测试了其他元素上的代码,<thead>
,<table>
,甚至像<span class="price">
这样的代码,它们都可以正常工作(ofcourse,删除“,0”代码失败)。他们都给出了正确的部分。 Outertext同上。但这一切都失败了<tbody>
。
现在,我已经浏览了解析器,但我不确定我能弄明白。我注意到,<thead>
甚至没有提及,但它工作正常。 耸肩
我想我可以尝试做儿童导航,但似乎也有小故障。我刚试过跑步:
$el = $html->find('table',0);
$el2 = $el->children(2);
echo $el2->outertext;
并没有骰子。试着用first_child
替换children
,用1替换2,仍然没有骰子。有趣的是,如果我尝试->find
而不是children
,它可以很好地工作。
我非常有信心我可以找到一个解决方案,但这种行为似乎很古怪,可以在这里发布。我好奇的心情很高兴能够得到所有的帮助。
答
确保您的tbody
来自一些JavaScript执行。我遇到了span标签的相同问题。后来我发现,如果有任何html代码通过jquery /任何其他JavaScript执行进入页面,那么在这种情况下,simple_html_dom
就会失败。
答
在simple_html_dom.php文件注释
或删除线#396
// if ($m[1]==='tbody') continue;
答
确保TBODY真的是存在的。许多浏览器会在检查面板中添加一个表格,尽管它们不在响应中。
答
没有为这里该问题的错误报告: http://sourceforge.net/p/simplehtmldom/bugs/79/
它仍然是在写这篇文章的时候开放。还有,如果你不希望修改源代码,例如在一个循环中找到<tr>
的
<?php
// The *BROKEN* way to find the <tr>'s
// below the <tbody> below the <table id="foo">
foreach($dom->find('tbl#foo tbody tr') as $tr) {
/* you will get nothing */
}
可以代替选择性地检查父标签名称,而迭代所有<tr>
的替代修复像这样:
<?php
// A workaround to find the <tr>'s
// below the <tbody> below the <table id="foo">
foreach($dom->find('tbl#foo tr') as $tr) { // note the lack of tbody selector
/* you will get all trs, but let's only work with ones with the parent
of a tbody! */
if($tr->parent->tag == 'tbody') { // our workaround
/* this part will work as you would expect the above broken code to work */
}
}
还要注意,我碰到了,那Chrome和FF检查员将纠正关于<tbody>
和<thead>
标签汤稍不相关的问题。要小心 - 只能看到实际的源代码 - 如果遇到无法解释的问题,请远离DOM检查员。参考文献:
+0
您已保存我的夜晚男人我不知道为什么你没有任何打击,但没有更改帮手文件,你的解决方案是最好的。谢谢。 –
2016-10-27 21:25:31
我使用PHP简单的HTML DOM解析器刮网上商店(也运行XAMPP 1.7.2与PHP5.3.0)的一些数据,和我正在与<tbody>
标签问题。该表的结构,essentialy(细节并不真的那么重要):<tbody>毛刺在PHP简单的HTML DOM解析器
<table>
<thead>
<!--text here-->
</thead>
<tbody>
<!--text here-->
</tbody>
</table>
现在,我尝试使用代码来获取到<tbody>
部分:
$element = $html->find('tbody',0)->innertext;
一点也没有不会抛出任何错误,当我试图回显它时,它不会打印任何内容。我已经测试了其他元素上的代码,<thead>
,<table>
,甚至像<span class="price">
这样的代码,它们都可以正常工作(ofcourse,删除“,0”代码失败)。他们都给出了正确的部分。 Outertext同上。但这一切都失败了<tbody>
。
现在,我已经浏览了解析器,但我不确定我能弄明白。我注意到,<thead>
甚至没有提及,但它工作正常。 耸肩
我想我可以尝试做儿童导航,但似乎也有小故障。我刚试过跑步:
$el = $html->find('table',0);
$el2 = $el->children(2);
echo $el2->outertext;
并没有骰子。试着用first_child
替换children
,用1替换2,仍然没有骰子。有趣的是,如果我尝试->find
而不是children
,它可以很好地工作。
我非常有信心我可以找到一个解决方案,但这种行为似乎很古怪,可以在这里发布。我好奇的心情很高兴能够得到所有的帮助。
确保您的tbody
来自一些JavaScript执行。我遇到了span标签的相同问题。后来我发现,如果有任何html代码通过jquery /任何其他JavaScript执行进入页面,那么在这种情况下,simple_html_dom
就会失败。
或删除线#396
// if ($m[1]==='tbody') continue;
确保TBODY真的是存在的。许多浏览器会在检查面板中添加一个表格,尽管它们不在响应中。
没有为这里该问题的错误报告: http://sourceforge.net/p/simplehtmldom/bugs/79/
它仍然是在写这篇文章的时候开放。还有,如果你不希望修改源代码,例如在一个循环中找到<tr>
的
<?php
// The *BROKEN* way to find the <tr>'s
// below the <tbody> below the <table id="foo">
foreach($dom->find('tbl#foo tbody tr') as $tr) {
/* you will get nothing */
}
可以代替选择性地检查父标签名称,而迭代所有<tr>
的替代修复像这样:
<?php
// A workaround to find the <tr>'s
// below the <tbody> below the <table id="foo">
foreach($dom->find('tbl#foo tr') as $tr) { // note the lack of tbody selector
/* you will get all trs, but let's only work with ones with the parent
of a tbody! */
if($tr->parent->tag == 'tbody') { // our workaround
/* this part will work as you would expect the above broken code to work */
}
}
还要注意,我碰到了,那Chrome和FF检查员将纠正关于<tbody>
和<thead>
标签汤稍不相关的问题。要小心 - 只能看到实际的源代码 - 如果遇到无法解释的问题,请远离DOM检查员。参考文献:
您已保存我的夜晚男人我不知道为什么你没有任何打击,但没有更改帮手文件,你的解决方案是最好的。谢谢。 – 2016-10-27 21:25:31
仅供参考:在'1.11'版本(修订版184)中,该代码在'629'行。 – h2ooooooo 2013-07-30 11:08:07
这应该在文档'>中提到:(' – 2013-08-02 16:38:19
在版本1.5(196版本)中,代码位于695行 – 2014-12-06 23:15:47