2014-10-22 194 views
1

我一直在玩PHP简单的HTML DOM解析器手册这里http://simplehtmldom.sourceforge.net/manual.htm发现,我买了一些测试的成功,除了这一个:PHP简单的HTML DOM解析器 - 解析嵌套元素

它得到了嵌套表和跨度和我想要用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,但我不能。

任何想法?任何帮助是极大的赞赏。谢谢。

+0

你应该张贴的HTML你的问题,而不是卷曲代码。 – pguardiario 2014-10-23 00:42:49

回答

1

载入网址。那么在这种情况下使用->innertext

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

它的工作!谢谢@Ghost – anagnam 2014-10-22 11:30:47

+0

@reymangana确定男人很高兴这有帮助 – Ghost 2014-10-22 11:31:16

+0

如果您不介意,我可以与您快速聊天吗? – anagnam 2014-10-22 11:47:06

0

你需要innertext。

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

outertext妥善首先返回<span class="mynum">123456</span>

+0

@MIvanlsten,我试过了,但仍然返回NULL。 – anagnam 2014-10-22 11:22:57

+0

你确定你有那个跨度的123456吗?也许垃圾邮件是空的,所以显然没有结果返回... – 2014-10-22 11:25:01

+0

@AresDraguna是的。请检查此网址http://relumastudio.com/test/target.html – anagnam 2014-10-22 11:25:55