开发者

How can I get all amazon category products?

How can I get all amazon products from an existing category?

With the API, I can browse 10 pages and get for each page 10 products.

The category has 502348 products, and I would like to get them all.

Here is my code:

Amazon Product Advertising API <?php ?>
$params = array(
        'Operation' => 'Item开发者_StackOverflow社区Search',
        'SearchIndex'=>'Electronics',
        //'BrowseNode'=>'281052',
        'ResponseGroup'=>'small',
        //'MerchantId' => 'All',
        //'Condition'=>'New',
        'ItemPage'=>'1471',


Maybe you should try to add two additional search parameters in your query - MinimumPrice and MaximumPrice - in combination with the keywords, of course. Then, whenever you get more than 10 pages for your search criteria, you should correct your min/mix price values.

That's how I was able to "avoid" that unreasonable limit that amazon set.


Use the Sitemaps from the Amazon Robots.txt:

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


You're seeing a new limitation imposed by Amazon. From the Product Advertising API home page:

ItemPage parameter will be limited to a maximum of 10 pages for ItemSearch results


Use a loop, pulling all products from each page until there are no more pages.


<?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;";
                    }
                }
                */
    }
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜