Amazon ECS API 获取 1000 本畅销书

Posted

技术标签:

【中文标题】Amazon ECS API 获取 1000 本畅销书【英文标题】:Amazon ECS API to fetch 1000 top selling books 【发布时间】:2011-10-19 08:34:02 【问题描述】:

我需要 API 来获取亚马逊上最畅销的书籍。默认it only fetches the top 10 items,但我需要10多个,使用单次点击接近1000项内容。

我需要一种方法来使用 Amazon ECS API 抓取 1000 种最畅销的书籍。

除了 Amazon API 之外,还有其他方法可以抓取亚马逊上所有最畅销的书籍吗?

【问题讨论】:

您的问题标题为“100”(10^2),但您的问题文本为“1000”(10^3)。请更正不准确的地方。 我认为标题和文字都说 1000。 查看编辑历史。您正在回复已超过 18 个月的评论,并且自发表后情况发生了变化。 【参考方案1】:

要获得 100 本畅销书,您必须指定 ItemPage 参数:

http://ecs.amazonaws.com/onca/xml?
Service=AWSECommerceService&
AWSAccessKeyId=[AWS Access Key ID]&
Operation=ItemSearch&
BrowseNode=17&
SearchIndex=Books&
ItemPage=2
&Sort=salesrank
&Timestamp=[YYYY-MM-DDThh:mm:ssZ]
&Signature=[Request Signature]

问题:最多可以返回 10 个 ItemPage。所以你不能超过 100 本书。

参考:http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?ItemSearch.html

【讨论】:

其实我知道怎么从亚马逊拉这10件,但是我需要一个办法从亚马逊拉这1000件,有没有其他办法拉出来 @对于特定的搜索参数只有 100。 我知道每个请求只能退回10个产品,但是如何检索10-20、20-30等产品? 只是想知道http://ecs.amazonaws.com/onca/xml? Service=AWSECommerceService... 是免费的 API 还是付费的或有免费限制?我想用它来搜索使用 ISBN 的书籍,而亚马逊在我提供信用卡之前不允许我创建 API 密钥。 应该是免费的api。我想,我还没有把信用卡信息放回 18 个月【参考方案2】:

这是我的做法 - 但由于亚马逊将 ItemPage 限制为 10 个,因此本月底后它将无法处理超过 100 件商品。

rescheck = Amazon::Ecs.item_search("search term here", :response_group => 'Large', :country => 'uk')
n=0
rescheck.total_pages.times do |n|
  n=n+1
  if n <= rescheck.total_pages
      res = Amazon::Ecs.item_search("search term here", :response_group => 'Large', :item_page =>n, :country => 'uk')
        res.items.each do |item|

          asin = item.get('ASIN')
          title = item.get('ItemAttributes/Title')
          brand = item.get('ItemAttributes/Brand')

 #etc

【讨论】:

【参考方案3】:

使用之前的评论,我从文档的 DOM 中删除了 IN 这个类别:

   [  
     
      "index":"All",
      "node":""
   ,
     
      "index":"Baby",
      "node":"1571275031"
   ,
     
      "index":"Beauty",
      "node":"1355017031"
   ,
     
      "index":"Books",
      "node":"976390031"
   ,
     
      "index":"Automotive",
      "node":"4772061031"
   ,
     
      "index":"Apparel",
      "node":"1571272031"
   ,
     
      "index":"PCHardware",
      "node":"976393031"
   ,
     
      "index":"Electronics",
      "node":"976420031"
   ,
     
      "index":"GiftCards",
      "node":"3704983031"
   ,
     
      "index":"Grocery",
      "node":"2454179031"
   ,
     
      "index":"HealthPersonalCare",
      "node":"1350385031"
   ,
     
      "index":"HomeGarden",
      "node":"2454176031"
   ,
     
      "index":"Industrial",
      "node":"5866079031"
   ,
     
      "index":"Jewelry",
      "node":"1951049031"
   ,
     
      "index":"KindleStore",
      "node":"1571278031"
   ,
     
      "index":"Luggage",
      "node":"2454170031"
   ,
     
      "index":"DVD",
      "node":"976417031"
   ,
     
      "index":"Music",
      "node":"976446031"
   ,
     
      "index":"MusicalInstruments",
      "node":"3677698031"
   ,
     
      "index":"OfficeProducts",
      "node":"2454173031"
   ,
     
      "index":"PetSupplies",
      "node":"4740420031"
   ,
     
      "index":"Shoes",
      "node":"1571284031"
   ,
     
      "index":"Software",
      "node":"976452031"
   ,
     
      "index":"SportingGoods",
      "node":"1984444031"
   ,
     
      "index":"Toys",
      "node":"1350381031"
   ,
     
      "index":"VideoGames",
      "node":"976461031"
   ,
     
      "index":"Watches",
      "node":"1350388031"
   
]

【讨论】:

【参考方案4】:
<?php
namespace MarcL;

use MarcL\CurlHttpRequest;
use MarcL\AmazonUrlBuilder;
use MarcL\Transformers\DataTransformerFactory;

class AmazonAPI

    private $urlBuilder = NULL;
    private $dataTransformer = NULL;
    public $item=0;
    public $perRequest=0;
    public function IND_money_format($money)
    
        $len = strlen($money);
        $m = '';
        $money = strrev($money);
        for($i=0;$i<$len;$i++)
            if(( $i==3 || ($i>3 && ($i-1)%2==0) )&& $i!=$len)
                $m .=',';
            
            $m .=$money[$i];
        
        return strrev($m);
    
    // Valid names that can be used for search
    private $mValidSearchNames = array(
        'All',
        'Apparel',
        'Appliances',
        'Automotive',
        'Baby',
        'Beauty',
        'Blended',
        'Books',
        'Classical',
        'DVD',
        'Electronics',
        'Grocery',
        'HealthPersonalCare',
        'HomeGarden',
        'HomeImprovement',
        'Jewelry',
        'KindleStore',
        'Kitchen',
        'Lighting',
        'Marketplace',
        'MP3Downloads',
        'Music',
        'MusicTracks',
        'MusicalInstruments',
        'OfficeProducts',
        'OutdoorLiving',
        'Outlet',
        'PetSupplies',
        'PCHardware',
        'Shoes',
        'Software',
        'SoftwareVideoGames',
        'SportingGoods',
        'Tools',
        'Toys',
        'VHS',
        'Video',
        'VideoGames',
        'Watches'
    );

    private $mErrors = array();

    public function __construct($urlBuilder, $outputType) 
        $this->urlBuilder = $urlBuilder;
        $this->dataTransformer = DataTransformerFactory::create($outputType);
    

    public function GetValidSearchNames() 
        return $this->mValidSearchNames;
    

    /**
     * Search for items
     *
     * @param   keywords            Keywords which we're requesting
     * @param   searchIndex         Name of search index (category) requested. NULL if searching all.
     * @param   sortBy              Category to sort by, only used if searchIndex is not 'All'
     * @param   condition           Condition of item. Valid conditions : Used, Collectible, Refurbished, All
     *
     * @return  mixed               SimpleXML object, array of data or false if failure.
     */
    public function ItemSearch($keywords,$itemPage, $searchIndex = NULL, $sortBy = NULL, $condition = 'All',$minPrice=50000,$maxPrice=55000) 
        ?>
        <table cellpadding="5">
            <thead>
                <tr>
                    <td>Title</td>
                    <td>List Price</td>
                    <td>Offer Price</td>
                    <td>Offer Selling Price</td>
                    <td>Amount Saved</td>
                    <td>Brand Name</td>
                    <td>Size</td>
                    <td>Color</td>
                    <td>Manufacturer</td>
                </tr>
            </thead>
            <tbody> 
            <?php

        $totalPages=0;
        while($maxPrice<=100000)
        
            $finished=false;
            $itemPage=0;
            while(!$finished)
            
                $itemPage=$itemPage+1;
                sleep(1);
                $mer="MerchantId";
                $merVal="Amazon";
                $params = array(
                    'Operation' => 'ItemSearch',
                    'ResponseGroup' => 'Small,ItemAttributes,Offers,OfferSummary',
                    'Keywords' => $keywords,
                    'Condition' => $condition,
                    'ItemPage' => $itemPage,
                    'ListPrice' => $itemPage,
                    'MinimumPrice' => $minPrice,
                    'MaximumPrice' => $maxPrice,
                    'SearchIndex' => empty($searchIndex) ? 'All' : $searchIndex,
                    'Sort' => $sortBy && ($searchIndex != 'All') ? $sortBy : NULL
                );
                $totalPages=$this->FetchItems($params,$itemPage,$maxPrice,false);
                if(($itemPage)==1)
                
                    $finished=true;
                    $itemPage=0;
                
            
            $minPrice=$maxPrice;
            $maxPrice=$maxPrice+5000;
        
        //echo "<br/>total Records : ".$this->item;
        ?>
            </tbody>
        </table>
        <br/><br/>

        <?php
        $style="";
                for($looper=1;$looper<=$totalPages;$looper++)
                
                    if($looper>($itemPage-3) && $looper<($itemPage+3))
                    
                        if($looper==$itemPage)
                        
                            $style="style='color:red;'";
                            echo "<a href='examples.php?itemPage=".$looper."' ".$style.">".$looper."</a>&nbsp;&nbsp;&nbsp;";
                        
                        else 
                        
                            echo "<a href='examples.php?itemPage=".$looper."'>".$looper."</a>&nbsp;&nbsp;&nbsp;";
                        
                    else if($looper>($totalPages-3))
                    

                        echo "<a href='examples.php?itemPage=".$looper."'>".$looper."</a>&nbsp;&nbsp;&nbsp;";
                    else if($looper>(($totalPages/2)-3) && $looper<(($totalPages/2)+3))
                    

                        echo "<a href='examples.php?itemPage=".$looper."'>".$looper."</a>&nbsp;&nbsp;&nbsp;";
                    
                
        die();
        //return $this->MakeAndParseRequest($params,$itemPage);
    

    /**
     * Lookup items from ASINs
     *
     * @param   asinList            Either a single ASIN or an array of ASINs
     * @param   onlyFromAmazon      True if only requesting items from Amazon and not 3rd party vendors
     *
     * @return  mixed               SimpleXML object, array of data or false if failure.
     */
    public function ItemLookup($asinList,$itemPage, $onlyFromAmazon = false) 
        $asinList="B01D0XDW1C";
        if (is_array($asinList)) 
            $asinList = implode(',', $asinList);
        

        $params = array(
            'Operation' => 'ItemLookup',
            'ResponseGroup' => 'ItemAttributes,Offers,Images',
            'ReviewSort' => '-OverallRating',
            'ItemId' => $asinList,
            'MerchantId' => ($onlyFromAmazon == true) ? 'Amazon' : 'All'
        );

        return $this->MakeAndParseRequest($params,$itemPage,true);
    

    public function GetErrors() 
        return $this->mErrors;
    

    private function AddError($error) 
        array_push($this->mErrors, $error);
    
    public function FetchItems($params,$itemPage,$maxPrice,$lookup=false)
    
        $signedUrl = $this->urlBuilder->generate($params);
            if($lookup)
            
                try 
                
                    $request = new CurlHttpRequest();
                    $response = $request->execute($signedUrl);
                    $fileContents = str_replace(array("\n", "\r", "\t"), '', $response);
                    $fileContents = trim(str_replace('"', "'", $fileContents));
                    $simpleXml = simplexml_load_string($fileContents);
                    $json = json_encode($simpleXml);
                    $decodedJson=json_decode($json,true);


                        //print_r($decodedJson);
                    print_r($decodedJson);
                    die();
                    $parsedXml = simplexml_load_string($response);

                    if ($parsedXml === false) 
                        return false;
                    

                    return $this->dataTransformer->execute($parsedXml);
                 catch(\Exception $error) 
                $this->AddError("Error downloading data : $signedUrl : " . $error->getMessage());
                return false;
                
            
            else
            
                try 
                
                    $request = new CurlHttpRequest();
                    $response = $request->execute($signedUrl);
                    $fileContents = str_replace(array("\n", "\r", "\t"), '', $response);
                    $fileContents = trim(str_replace('"', "'", $fileContents));
                    $simpleXml = simplexml_load_string($fileContents);
                    $json = json_encode($simpleXml);
                    $decodedJson=json_decode($json,true);
                    //print_r($decodedJson);
                    //die();
                    if(isset($decodedJson['Items']))
                    
                        $this->perRequest=0;
                        foreach($decodedJson['Items']['Item'] as $itm)
                        
                            if(isset($itm['ItemAttributes']['ListPrice']['FormattedPrice']))
                            
                                $this->item=$this->item+1;
                                $this->perRequest=$this->perRequest+1;

                                ?>
                                <tr>
                                    <td>
                                        <?php
                                            if(isset($itm['ItemAttributes']['Title']))
                                                echo $itm['ItemAttributes']['Title'];
                                            else 
                                                echo "N/A";
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                            if(isset($itm['ItemAttributes']['ListPrice']['FormattedPrice']))
                                                echo $itm['ItemAttributes']['ListPrice']['FormattedPrice'];
                                            else 
                                                echo "N/A";
                                        ?>                          
                                    </td>
                                    <?php
                                        $savedAmount=0;
                                        if(isset($itm['Offers']['Offer']['OfferListing']['Price']['FormattedPrice']))
                                        
                                            ?>
                                                <td><?php echo $itm['Offers']['Offer']['OfferListing']['Price']['FormattedPrice']; ?></td>
                                            <?php
                                            if(isset($itm['Offers']['Offer']['OfferListing']['SalePrice']['FormattedPrice']))
                                            
                                                $total=(int)($itm['ItemAttributes']['ListPrice']['Amount']);
                                                $offer=(int)($itm['Offers']['Offer']['OfferListing']['SalePrice']['Amount']);
                                                $savedAmount=$total-$offer;
                                                $savedAmount=$savedAmount/100;
                                                $savedAmount=$this->IND_money_format($savedAmount);
                                                $savedAmount="INR ".$savedAmount.".00";
                                                ?>
                                                    <td><?php echo $itm['Offers']['Offer']['OfferListing']['SalePrice']['FormattedPrice']; ?></td>
                                                    <td><?php echo $savedAmount; ?></td>
                                                <?php
                                            
                                            else
                                            
                                                $total=(int)($itm['ItemAttributes']['ListPrice']['Amount']);
                                                $offer=(int)($itm['Offers']['Offer']['OfferListing']['Price']['Amount']);
                                                $savedAmount=$total-$offer;
                                                $savedAmount=$savedAmount/100;
                                                $savedAmount=$this->IND_money_format($savedAmount);
                                                $savedAmount="INR ".$savedAmount.".00";
                                                ?>
                                                <td><?php echo $itm['Offers']['Offer']['OfferListing']['Price']['FormattedPrice']; ?></td>
                                                <td><?php echo $savedAmount; ?></td>
                                                <?php
                                            
                                        
                                        else if(isset($itm['OfferSummary']['LowestNewPrice']['FormattedPrice']))
                                        
                                            $total=(int)($itm['ListPrice']['Amount']);
                                            $offer=(int)($itm['Offers']['Offer']['OfferListing']['SalePrice']['Amount']);
                                            $savedAmount=$total-$offer;
                                            $savedAmount=$savedAmount/100;
                                            $savedAmount=$this->IND_money_format($savedAmount);
                                                $savedAmount="INR ".$savedAmount.".00";
                                            ?>
                                                <td><?php echo $itm['OfferSummary']['LowestNewPrice']['FormattedPrice']; ?></td>
                                                <td><?php echo $itm['OfferSummary']['LowestNewPrice']['FormattedPrice']; ?></td>
                                                <td><?php echo $savedAmount; ?></td>
                                            <?php
                                        
                                        else
                                        
                                            ?>
                                                <td>N/A</td>
                                                <td>N/A</td>
                                                <td>N/A</td>
                                            <?php
                                        
                                    ?>  
                                    <td>
                                        <?php
                                            if(isset($itm['ItemAttributes']['Brand']))
                                                echo $itm['ItemAttributes']['Brand'];
                                            else 
                                                echo "N/A";
                                        ?>                      
                                    </td>

                                    <td>
                                        <?php
                                            if(isset($itm['ItemAttributes']['Size']))
                                                echo $itm['ItemAttributes']['Size'];
                                            else 
                                                echo "N/A";
                                        ?>                      
                                    </td>
                                    <td>
                                        <?php
                                            if(isset($itm['ItemAttributes']['Color']))
                                                echo $itm['ItemAttributes']['Color'];
                                            else 
                                                echo "N/A";
                                        ?>              
                                    </td>
                                    <td>
                                        <?php
                                            if(isset($itm['ItemAttributes']['Manufacturer']))
                                                echo $itm['ItemAttributes']['Manufacturer'];
                                            else 
                                                echo "N/A";
                                        ?>                              
                                    </td>
                                </tr>
                                <?php

                            
                        
                        //return 
                        //echo $maxPrice." : ".$decodedJson['Items']['TotalPages']."<br/>";
                    
                    //echo "PerRequest : ".$this->perRequest."<br/>";
                    //die();
                    //$parsedXml = simplexml_load_string($response);

                    //if ($parsedXml === false) 
                    //  return false;
                    //

                    //return $this->dataTransformer->execute($parsedXml);
                 catch(\Exception $error) 
                $this->AddError("Error downloading data : $signedUrl : " . $error->getMessage());
                return false;
                
            
    
    private function MakeAndParseRequest($params,$itemPage,$lookup=false) 
    
        $this->item=0;

                /*$style="";
                for($looper=1;$looper<=$totalPages;$looper++)
                
                    if($looper>($itemPage-3) && $looper<($itemPage+3))
                    
                        if($looper==$itemPage)
                        
                            $style="style='color:red;'";
                            echo "<a href='examples.php?itemPage=".$looper."' ".$style.">".$looper."</a>&nbsp;&nbsp;&nbsp;";
                        
                        else 
                        
                            echo "<a href='examples.php?itemPage=".$looper."'>".$looper."</a>&nbsp;&nbsp;&nbsp;";
                        
                    else if($looper>($totalPages-3))
                    

                        echo "<a href='examples.php?itemPage=".$looper."'>".$looper."</a>&nbsp;&nbsp;&nbsp;";
                    else if($looper>(($totalPages/2)-3) && $looper<(($totalPages/2)+3))
                    

                        echo "<a href='examples.php?itemPage=".$looper."'>".$looper."</a>&nbsp;&nbsp;&nbsp;";
                    
                
                */
    

?>

【讨论】:

只需更改价格范围并获得您想要的数量 您可以从github.com/MarcL/AmazonProductAPI 获取API 并如上所述对Amazon.php 文件进行更改,这完全可以解决。

以上是关于Amazon ECS API 获取 1000 本畅销书的主要内容,如果未能解决你的问题,请参考以下文章

提供最新 amazon-ecs-optimized image 的 ami-id 的脚本或 api

ELB 和 ECS 集群前的 Amazon API Gateway

在 Amazon ECS 上的 Docker 中运行节点 API 的最佳方式是啥?

在 Jenkins 上使用 Groovy 配置 amazon-ecs 从属插件

错误 CS0030:无法在 Amazon Web Service 中将类型“Simple.Amazon.ECS.ImageSet[]”转换为“Simple.Amazon.ECS.ImageSet”

Amazon ECS:对于 Jenkins amazon-ecs-plugin 的给定值不存在 Fargate 配置