2011-04-09 24 views
1

我試圖從亞馬遜網頁上的產品刮取價格數據,但我得到的不僅僅是變量中的價格數據,還包含了其他元素作爲<span>等代碼...如何從簡單的HTML DOM分析器刮刮具體數據

include 'simple_html_dom.php'; 
$html1 = file_get_html('http://www.amazon.co.uk/New-Apple-iPod-touch-Generation/dp/B0040GIZTI/ref=br_lf_m_1000333483_1_1_img?ie=UTF8&s=electronics&pf_rd_p=229345967&pf_rd_s=center-3&pf_rd_t=1401&pf_rd_i=1000333483&pf_rd_m=A3P5ROKL5A1OLE&pf_rd_r=1ZW9HJW2KN2C2MTRJH60'); 

$price_data1 = $html1->find('b[class=priceLarge]',0); 

變量則還包括諸如<b class="priceLarge">£163.00</b>

有沒有辦法來進行修剪不需要的數據數據?我只需要£163.00。

我不確定在查找過程中是否執行此操作,或者當我回顯變量時,是否指定我想要的內容?

乾杯

回答

1

更改您的XPath來選擇text()孩子<b>元素的,而不是選擇元素本身。

$price_data1 = $html1->find('b[class=priceLarge]/text()',0); 
-1
<b class="priceLarge">£163.00</b> 

只需使用以下命令:

$p = "/b class=\"priceLarge\">(.*)<\/b>/"; 
preg_match($p, $html, $match) 
0

你可以嘗試網上API像Synthetics Web。您可以使用最少的編碼工作來提取數據。

 
$url = urlencode('http://www.amazon.co.uk/New-Apple-iPod-touch-Generation/dp/B0040GIZTI/ref=br_lf_m_1000333483_1_1_img?ie=UTF8&s=electronics&pf_rd_p=229345967&pf_rd_s=center-3&pf_rd_t=1401&pf_rd_i=1000333483&pf_rd_m=A3P5ROKL5A1OLE&pf_rd_r=1ZW9HJW2KN2C2MTRJH60'); 
$wid = '160'; 

$data = json_decode(file_get_contents("http://www.syntheticsweb.com/resources/www.json?wid=$wid&url=$url")); 

echo $data->price; 
+1

迄今爲止所有的答案似乎都推薦合成網站。你有什麼關係嗎?如果是這樣,你將不得不在你的答案中明確說明你的聯繫,正如FAQ中所述。 – Bart 2012-09-17 10:01:20

+0

該服務已停止供貨。 – TheRealJAG 2014-05-16 16:11:14

1

只使用

$result=$price_data1->innertext; 

你一定會得到期望的輸出。