问题
I'm pretty new to PHP, so i try to use Simple HTML DOM Parser to get the information i need from a website. here is the sample node:
<div>
<h1>Hello</h1>
<figure id="XXX">
<div class="abc">ABC</div>
<div class="qwe">QWE</div>
<div class="zxc">ZXC</div>
</figure>
</div>
$element contain above node. What i need to get is the "QWE", so i try:
$name = $element->find('figure[id=XXX]')->children(1)->innertext;
but now the problem: Fatal error: Call to a member function children() on a non-object , that is no problem if i stop at find('figure[id=XXX]'), but it return array and i can't get the exact information.
Any solution? Please help, thanks in advance.
回答1:
There you go:
$str = '<div>
<h1>Hello</h1>
<figure id="XXX">
<div class="abc">ABC</div>
<div class="qwe">QWE</div>
<div class="zxc">ZXC</div>
</figure>
</div>';
$html = str_get_html($str);
$str = $html->find('figure[id=XXX]',0)->children(1)->plaintext;
echo($str);
find returns elements and you need single element so set index 0 (the first one) at the second parameter in find
回答2:
It's better to do:
$html->find('figure#XXX > *[2]',0)->plaintext;
来源:https://stackoverflow.com/questions/16147044/php-simple-html-dom-parser-call-to-a-member-function-children-on-a-non-object