簡體   English   中英

如何在沒有Amazon API的情況下從Amazon Url提取價格

[英]How to extract price from Amazon Url without Amazon API

我正在嘗試從Amazon URL加載html文件,以使用Yii上的簡單php函數提取產品價格。 我開始使用php函數file_get_contents獲取整個文件,然后從帶有DOM的html文件中僅提取價格。

我正在使用DOM解析器來讀取HTML文件。 它具有讀取html文件標簽的便捷功能。 這是解析器:

http://simplehtmldom.sourceforge.net/

php分析的URL可以是amazon.com,amazon.co.uk,amazon.it等。將來,此功能還將用於分析與Amazon不同的其他URL。

我創建了一個簡單的函數,可以從URL提取價格,這里是:

public function findAmazonPriceFromUrl($url) {
    Yii::import('ext.HtmlDOMParser.*');
    require_once('simple_html_dom.php');

    $html = file_get_html($url);
    $item = $html->getElementsById('actualPriceValue');
    if ($item) {
        $price = $item[0]->firstChild()->innertext;
    } else {
        $item = $html->getElementsById('current-price');
        $price = $item[0]->innertext;
    }
    return $price;
}

file_get_html函數如下:

function file_get_html($url) {
    $dom = new simple_html_dom();
    $contents = file_get_contents($url);
    if (empty($contents) || strlen($contents) > MAX_FILE_SIZE) {
        return false;
    }
$dom->load($contents);
return $dom;

}

我注意到在幾次請求(各種鏈接)之后,我總是從服務器收到錯誤消息(錯誤500)。 我檢查了我的apache日志文件,但是一切都很好。

亞馬遜可以在一段時間后阻止我的請求嗎? 我該如何解決?

先謝謝您的幫助

我遇到了同樣的問題,這是我的解決辦法:如果未解析圖片,我將再次運行腳本。 圖像首先在我的php腳本中解析,所以我檢查它是否有效並且Amazon提供信息。 希望對您有所幫助。

if($html->find('#main-image')) {    
   foreach($html->find('#main-image') as $e) {
      echo '<span href="'. $e->src . '" class="imgblock parseimg">
               <img src="'. $e->src . '" class="resultimg" alt="'.$name.'" title="'.$name.'">
            </span>
            <input type="hidden" name="my-item-img" value="'. $e->src . '" />';
   }
} else {
   gethtml($url,$domain);
   die;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM