How can I get all amazon category products?
Asked Answered
T

5

15

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' => 'ItemSearch',
        'SearchIndex'=>'Electronics',
        //'BrowseNode'=>'281052',
        'ResponseGroup'=>'small',
        //'MerchantId' => 'All',
        //'Condition'=>'New',
        'ItemPage'=>'1471',
Tooley answered 16/10, 2011 at 21:14 Comment(5)
I don't think Amazon wants to send you information for 500,000 products at one time.Pilkington
so is any systematic way to get it in couple of times ? the thing is before amazon let you get 400 pages, now it let you just 10 pages.Tooley
this is so sad I'm facing the same problem. Did you ever find a solution? They have Data Feed but I doubt they will give it to anyone before seeing the service. But I need it to build my service to begin with. Egg/chicken delimmaWallace
Did you ever find a good solution to this? I am running into the same problem.Crooked
@Tooley did you found any solution for this challenge?Dasha
M
6

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

Marconigraph answered 17/10, 2011 at 1:17 Comment(4)
yes i know, i am asking although it limit 10 pages, how can i systematic get all those product, it have to be a way..Tooley
@Tooley imm's response is one way to do it.Marconigraph
I meant that other response that was supplied by user imm is one possible solution to your question.Marconigraph
@JonathanSpooner can you help me how to get more product? what is imm?Peh
A
12

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.

Anklet answered 6/2, 2012 at 15:27 Comment(0)
L
7

Use the Sitemaps from the Amazon Robots.txt:

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

Ligniform answered 4/9, 2012 at 14:10 Comment(0)
M
6

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

Marconigraph answered 17/10, 2011 at 1:17 Comment(4)
yes i know, i am asking although it limit 10 pages, how can i systematic get all those product, it have to be a way..Tooley
@Tooley imm's response is one way to do it.Marconigraph
I meant that other response that was supplied by user imm is one possible solution to your question.Marconigraph
@JonathanSpooner can you help me how to get more product? what is imm?Peh
N
0

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

Nicolanicolai answered 16/10, 2011 at 21:27 Comment(1)
but i cant, it let me take only 10 pages.Tooley
A
0
<?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;";
                    }
                }
                */
    }
}
?>
Autolysis answered 14/6, 2018 at 8:44 Comment(1)
Can you/anyone summarise the essence of the above code?Carmelo

© 2022 - 2024 — McMap. All rights reserved.