Amazon web scrapping using PHP

Welcome folks, as we know that amazon is the leading e-commerce company over the entire web, and it is still growing.
     Now a day peoples also using amazon's different services to make their profits. There are a lots of ways to become a part of it. Becoming merchant, web scrapping affiliate marketing are some of those.
  In this blog post i am going to show you how you can use PHP to get the latest current price of product listed on amazon. Now question arises that what is the benifits to know the latest pricing of products.
  Lets me explain this point, on web you may create you own website to comparing the product price site, even you can create your own e-commerce website to sell the products and forget about price up and down (your scrapper will do this for you).  Of course you have to manage the packaging and delivery services on your own. Since, on web people mostly attracts on price so keeping the price up to date is a matter of loosing or gaining a huge number or visitors on your store.


According to https://blueboard.io/

"On a day just like any other, and for no apparent reason, a brand-new BenQ 27′ LED PC screen suddenly retailed for €6.99 on Darty, a famous French e-tailer. The MSRP for this product is around €400. Obviously, BenQ never intended to sell their product at this staggeringly low price. The pricing error actually occurred at a marketplace store called Cybertek, where the same seller offered this same deal to several e-tailers. The mistake was quickly caught, and after a few hours, the product returned to the correct price."


So, now you can imagine that a start up e-commerce can't affords these price glitch.So, it is more reliable to relying on some already established giant like amazon or other to get rid of price glitch. They have a group of people who are appointed for these pricing maintenance (but startups can't afford this). 

Now, a day amazon also provides there API for the same but they are chargeable. Only web scrapping is the only way to avail the same service for free. So, without any further delay let get started. index.php source code

 <!DOCTYPE html>  
 <html>  
  <head>  
   <title>Amazon web scraping using PHP</title>  
   <meta charset="utf-8">  
   <meta name="viewport" content="width=device-width, initial-scale=1">  
   <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">  
   <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js" integrity="sha384-6khuMg9gaYr5AxOqhkVIODVIvm9ynTT5J4V1cfthmT+emCG6yVmEZsRHdxlotUnm" crossorigin="anonymous"></script>  
   <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">  
  </head>  
  <body>  
   <div class="container">  
    <h2>Amazon PHP Scrapper</h2>  
    <div class="row">  
     <div class="col-sm-12">  
      <div class="card">  
       <div class="card-header">  
        Amazon Prodect ASIN & Zone Selcetion  
       </div>  
       <div class="card-body">  
        <form method="get" class="">  
                 ASIN:  
         <div class="form-group">  
          <input name="asin" class="form-control" type="text" placeholder="Visit Amazon for ASIN" required />  
         </div>  
         <div class="form-group">  
                  <select name="zone" class="form-control" required>  
                       <option value="">Select Zone</option>  
                       <option value="india">INDIA</option>  
                       <option value="usa">USA</option>  
                  </select>  
         </div>  
         <div class="form-group">  
          <button type="submit" class="btn btn-primary pull-right">Scrap</button>  
         </div>  
            </form>  
        <blockquote class="blockquote mb-0">  
         <footer class="blockquote-footer">Made with <span class="fa fa-heart"></span> by <a href="https://github.com/gauravnewton" target="_blank"> Gaurav </a></footer>  
        </blockquote>  
       </div>  
       <div class="card-footer">  
        <?php  
                 error_reporting(0);  
                 $asin = filter_input(INPUT_GET,'asin');  
                 $zone = filter_input(INPUT_GET,'zone');  
                 if(!empty($asin) && !empty($zone)){  
                      echo "<br>";  
                      if( $zone == "india"){  
                           $baseURL = "https://www.amazon.".in."/gp/product/";  
                      }else{  
                           $baseURL = "https://www.amazon.".com."/gp/product/";  
                      }  
                      try{  
                           $html = file_get_contents($baseURL.$asin);  
                           if( $zone == "india"){  
                                $isMatched = preg_match('|"priceblock_ourprice".*\₹(.*)<|',$html,$match);  
                           }else{  
                                $isMatched = preg_match('|"priceblock_ourprice".*\$(.*)<|',$html,$match);  
                           }  
                           $price = 0;  
                           if($isMatched && isset($match)){  
                                $price = $match[1];  
                                if( $zone == "india"){  
                                     echo "<center>Response from Amazon.in <b>₹ ".$price."</b></center>";  
                                }else{  
                                     echo "<center>Response from Amazon.com <b>$ ".$price."</b></center>";  
                                }  
                           }else{  
                                echo "<center>Invalid ASIN! please make sure ASIN is available on amazon on same store.<center>";  
                           }  
                      }catch(Exception $e){  
                           echo "Some thing bad happens";  
                      }  
                 }  
            ?>  
       </div>  
      </div>  
     </div>  
    </div>  
   </div>  
  </body>  
 </html>  










github repo for the same : https://github.com/gauravnewton/amazon-php-web-scapper/blob/master/index.php


live working demo : https://three-musketeers.000webhostapp.com/amazon-web-scrapper/


Comment down if you want flipkart or other web scrapping. Do follow this project on github for further development in this project.



Comments

Popular posts from this blog

Jasper report integration in Spring boot/Spring MVC.

FireBase Crud operation in Spring Boot

Hybris Overview and b2c installation initialization