2016-09-15 38 views
-1

我已經遍尋谷歌,試圖找出如何找到使用PHP的產品的價格。我如何能夠在PHP中獲得亞馬遜產品的價格?

我期待從產品URL

我不希望使用亞馬遜Web服務得到的價格作爲我的帳號被封,我期待做的更簡單的方法它。我並不是那麼擅長PHP,而我來自Python,我一直在想,你可能會使用類似正則表達式或匹配的東西(如果PHP中存在這些東西的話)。

+0

您是否在尋找剛纔得到的價格還是別的什麼嗎? – FluxCoder

+0

我只是爲了能夠從網址上獲得價格。 –

+0

使用http://simplehtmldom.sourceforge.net/來解析dom元素 – Thamaraiselvam

回答

0

這裏有一些PHP代碼可以獲取價格,但如果價格正在報價中,它將不會獲得該價格,它只會獲得亞馬遜產品的總價格。

//Grab the contents of the Product page from Amazon 
$source = file_get_contents("http://rads.stackoverflow.com/amzn/click/B01DFKC2SO"); 

//Find the Price (Searches using Regex) 
preg_match("'<span id=\"priceblock_ourprice\" class=\"a-size-medium a-color-price\">(.*?)</span>'si", $source, $match); 

//Check if it was actually found. 
if($match){ 
    //Echo Price if it was indeed found. 
    echo $match[1]; 
} 

我也不能確定,如果有可能既.com和亞馬遜的其他版本要做到這一點,你就必須做一些搜索正確的標籤,類別和/或ID。

編輯
作爲這個答案的意見要求,要拿到冠軍添加以下代碼:

//Find the Title (Searches using Regex) 
preg_match("'<span id=\"productTitle\" class=\"a-size-large\">(.*?)</span>'si", $source, $match); 
if($match){ 
    echo $match[1]; 
} 
+0

這是有效的,我知道我說我只想獲得價格,你能給我一些代碼來獲得產品的名字嗎? –

+0

當然,我會盡快更新我的答案。 – FluxCoder

0

您也可以使用PHPHtmlParser包。

PHPHtmlParser是一個簡單,靈活的html解析器,它允許你使用任何css選擇器(如jQuery)選擇標籤 。目標是 協助開發工具,它需要一個快速,簡單的方法來處理廢品html,無論它是否有效!這個項目是由sunra/php-simple-html-dom-parser支持的原始 ,但支持似乎 已停止,所以這個項目是我對他以前的工作的改編。

然後,你可以用它來提取價格做這樣的事情:

// of course you need to install the package, best done using composer 
include 'vendor/autoload.php'; 

use PHPHtmlParser\Dom; 

$dom = new Dom; 
// create a Dom object from the desired url 
$dom->loadFromUrl("http://rads.stackoverflow.com/amzn/click/B01LOP8EZC"); 
// extract the price from the Dom 
$price = $dom->find('#priceblock_ourprice'); 
print $price->text(); 
相關問題