PHP简单HTML DOM解析器-解析嵌套元素


PHP Simple HTML DOM parser - parsing nested elements

我一直在玩这里的PHP简单HTML DOM解析器手册http://simplehtmldom.sourceforge.net/manual.htm我在一些测试中取得了成功,除了这个:

它有嵌套的表和span,我想用mynum类解析span的外部文本。

<?php
require_once 'simple_html_dom.php';
$url = 'http://relumastudio.com/test/target.html';
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21");
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$DEBUG = 1;
if($DEBUG){
    $html = new simple_html_dom();
    $html->load($url);
    echo $html->find('span[class=mynum]',0)->outertext; // I should get 123456
}else{
    echo $result;
}        
curl_close($ch);

我以为我只需要打一次电话给echo $html->find('span[class=mynum]',0)->outertext;就可以得到文本123456,但我做不到。

有什么想法吗?非常感谢您的帮助。非常感谢。

首先正确加载url。然后在这种情况下使用->innertext

$url = 'http://relumastudio.com/test/target.html';
$html = file_get_html($url);
$num = $html->find('span.mynum', 0)->innertext;
echo $num;

您需要innertext。

$html = new simple_html_dom();
$html->load_file($url);
echo $html->find('span[class=mynum]',0)->innertext;

outertext返回<span class="mynum">123456</span>