如何获得所有亚马逊类别的产品

Posted

技术标签:

【中文标题】如何获得所有亚马逊类别的产品【英文标题】:how to get all amazon category products 【发布时间】:2011-12-08 21:34:06 【问题描述】:

如何从现有类别中获取所有亚马逊产品?

使用 API,我可以浏览 10 个页面,并为每个页面获取 10 个产品。

该类别有 502348 个产品,我想全部获取。

这是我的代码:

Amazon Product Advertising API <?php ?>
$params = array(
        'Operation' => 'ItemSearch',
        'SearchIndex'=>'Electronics',
        //'BrowseNode'=>'281052',
        'ResponseGroup'=>'small',
        //'MerchantId' => 'All',
        //'Condition'=>'New',
        'ItemPage'=>'1471',

【问题讨论】:

我不认为亚马逊想要一次向您发送 500,000 种产品的信息。 那么有什么系统的方法可以在几次内获得它吗?事情是以前亚马逊让你得到 400 页,现在它只给你 10 页。 这太可悲了,我面临着同样的问题。你有没有找到解决办法?他们有数据提要,但我怀疑他们会在看到服务之前将其提供给任何人。但我需要它来建立我的服务。鸡蛋/鸡delimma 你有没有找到一个好的解决方案?我遇到了同样的问题。 @Yan 你找到解决这个挑战的方法了吗? 【参考方案1】:

也许您应该尝试在查询中添加两个额外的搜索参数 - MinimumPriceMaximumPrice - 当然要结合 关键字 .然后,每当您的搜索条件超过 10 页时,您应该更正您的最低/混合价格值。

这就是我能够“避免”亚马逊设定的不合理限制的方式。

【讨论】:

您好,您能否提供工作代码以将所有产品归入一个类别?【参考方案2】:

使用来自Amazon Robots.txt 的站点地图:

http://www.amazon.com/sitemaps.US_detail_page_sitemap_desktop_index.xml.gz

【讨论】:

【参考方案3】:

您看到了亚马逊施加的新限制。来自产品广告 API home page:

ItemPage 参数将限制为 ItemSearch 结果最多 10 页

【讨论】:

是的,我知道,我在问虽然它限制了 10 页,但我怎样才能系统地获得所有这些产品,它必须是一种方式.. @Yan imm 的回应是一种方法。 我的意思是用户 imm 提供的其他回复是您问题的一种可能解决方案。 @JonathanSpooner 你能帮我如何获得更多产品吗? imm是什么?【参考方案4】:

使用循环,从每个页面拉出所有产品,直到没有更多页面。

【讨论】:

但我做不到,我只需要 10 页。【参考方案5】:
<?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,BrowseNodes',
                    '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;";
                    
                
                */
    

?>

【讨论】:

你/任何人能总结一下上面代码的本质吗?

以上是关于如何获得所有亚马逊类别的产品的主要内容,如果未能解决你的问题,请参考以下文章

从亚马逊库存中提取产品/价格/类别/规格

如何访问亚马逊类别列表的第 101 页

如何获取任何特定亚马逊商家/卖家的所有产品?

如何获取亚马逊 MWS 的所有产品列表

C# amazon 上传提要,将产品与现有产品匹配?

亚马逊产品广告 API - 获得评论排名