2011-01-20 357 views
0

好吧,這看起來很簡單,但我不能讓這個編碼在所有的工作...PHP的XPath屏幕抓取問題

$html = new DOMDocument(); 
@$html->loadHtmlFile('http://www.groupon.com/deals/the-newfoundland-shop'); 
$xpath = new DOMXPath($html); 
$nodelist = $xpath->query("/html/body/div[@id='global_container']/div[@id='main']/div[@id='content']/div/div[1]/div[2]/div[@id='number_sold_container']/table[1]/tbody/tr/td"); 
foreach ($nodelist as $n){ 
echo $n->nodeValue."\n"; 
} 
$buys = "$n->nodeValue"; 

請幫助之前,我失去它...

感謝

+2

*(tipp)* http://www.groupon.com/pages/api – Gordon 2011-01-20 23:02:17

回答

0

嘗試使用此語法的XPath -

/XHTML:HTML/XHTML:身體/ XHTML:DIV [@ ID = 'global_container'] ...

如果不要幫助檢查您的XPath是確定(這是很長的)

0

首先FAQ:頁面看起來正確的XHTML,所以你需要註冊XHTML命名空間URI http://www.w3.org/1999/xhtml一些前綴,並使用該前綴您的XPath表達。

第二常見問題:您應該注意,原始來源沒有tbody元素。這是由瀏覽器添加的。