2015-04-03 95 views
0

我有一個從Web服務解析HTML文檔PHP

<?php 
$speechText=$_POST['spokentext']; 
echo $speechText.'!'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://nlp.stanford.edu:8080/parser/index.jsp'); 
curl_setopt($ch, CURLOPT_POST, 3); 
curl_setopt($ch,CURLOPT_POSTFIELDS, "query=".$speechText."&parserSelect=English&parse=Parse"); 
$result = curl_exec($ch); 
//echo $result; 
curl_close($ch); 
$dom = new DOMDocument(); 
$dom->loadHTML($result); 
$xpath = new DOMXPath($dom); 
$result = $xpath->query('/html/body/div[2]/div[5]/pre'); 
echo $result->item(0)->nodeValue; 
?> 

提取信息的PHP代碼的變量$導致持有其返回的HTML文檔。我想通過解析它從中提取一些信息。我正在使用內置的DOM解析器,但是出現此錯誤 -

PHP Notice: Trying to get property of non-object 

什麼是錯誤?

P.S. - 我在編程的完整的n00b:P

+0

http://php.net/domdocument – cmbuckley 2015-04-03 16:22:35

+0

請:http://bit.ly/1NGQpUF – 2015-04-03 16:24:53

+0

@cmbuckley 一些簡單的幫助如何解析它?訪問特定的類/ ID? – GokuShanth 2015-04-03 16:26:10

回答

0

它相當簡單

$dom = new DOMDocument(); 
@$dom->loadHTML($html); 
$xpath = new DOMXpath($dom); 
$elements = $xpath->query("/html/body/div[2]/div[6]/pre"); 
$typed_dependencies_collapsed = array(); 
if (!is_null($elements)) { 
    foreach ($elements as $element) { 
    $nodes = $element->childNodes; 
    foreach ($nodes as $node) { 
     echo $node->nodeValue."\n"; 
     array_push($typed_dependencies_collapsed,$node->nodeValue); 
    } 
    } 
}