Jump to content

Recommended Posts

Hi All

 

I need a little help with my Amazon price scraping script. I currently sell over 3000 items on amazon and need to keep up to date when prices go down. This script uses a mySQL database, goes to the relevant amazon page then scrapes the lowest price. I can then check the prices against my inventory to make sure i'm not to expensive. The problem is that it continually uses the price from the first url scraped and doesn't update. If anyone can help I'd be very grateful.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 <head> 
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
   <title> 
     Untitled Document 
   </title> 
 </head> 
 <body> 
   <?php 
   // listing script 

   // connect to the server 
   mysql_connect( 'localhost','XXXXXXXXX','XXXXXXXXXX' ) 
   or die( "Error! Could not connect to database: " . mysql_error() ); 

   // select the database 
   mysql_select_db( XXXXXXXX) 
   or die( "Error! Could not select the database: " . mysql_error() ); 
       

   // retrieve all the rows from the database 
   $query = "SELECT * FROM `XXXXXXX`"; 

   $results = mysql_query( $query ); 

   // print out the results 
   if( $results ) 
   { 
       while( $contact = mysql_fetch_object( $results ) ) 
       { 
           // print out the info 
           $id = $contact -> id; 
           $sku = $contact -> sku; 
           $ASIN = $contact -> ASIN; 
           if( !( $data = file_get_contents("http://www.amazon.co.uk/gp/offer-listing/$ASIN/") ) ) 
           { 
             die("Could not create a connection to Amazon.co.uk"); 
           } 
           preg_match("/<span\sclass=\"price\">(.*)\s/i",$file,$match);
           $result = $match[1]; 
           ?> 
           <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
             <tr> 
               <td width="5%"> 
                 <?php echo($id) ?> 
               </td> 
               <td width="23%"> 
                 <?php echo($sku) ?> 
               </td> 
               <td width="18%"> 
                 <?php echo($ASIN) ?> 
               </td> 
               <td width="39%"> 
                 <?php echo($url) ?> 
               </td> 
               <td width="20%"> 
                 <?php echo($result) ?> 
               </td> 
             </tr> 
           </table> 
           <?php 
       } 
   } 
   else 
   { 
       die( "Trouble getting info from database: " . mysql_error() ); 
   } 

   ?> 
 </body> 
</html>

 

Please find a working link below.

 

http://www.amazon.co.uk/gp/offer-listing/B0001K9W9Y/

 

I think i'm ok using preg_match as I only want the first price on the page (I need to know what the lowest marketplace price is). I think the problem's near $match[1]. The array doesn't seem to update when a new product is selected from my database.

 

The result i get at the moment is

 

1   POBSIGN   B000PZGFPE   http://www.amazon.co.uk/gp/offer-listing/B000PZGFPE/   2.99 
2   POOH&TIGGERSHADE   B000PZ9L6Y   http://www.amazon.co.uk/gp/offer-listing/B000PZ9L6Y/   2.99 
3   PINKSTEERING   B000PZG9UK   http://www.amazon.co.uk/gp/offer-listing/B000PZG9UK/   2.99 
4   56027   B0001K9W9Y   http://www.amazon.co.uk/gp/offer-listing/B0001K9W9Y/   2.99 
5   13962   B0001K9PQ4   http://www.amazon.co.uk/gp/offer-listing/B0001K9PQ4/   2.99 
6   BATMANORGANISER   B000P8XM7K   http://www.amazon.co.uk/gp/offer-listing/B000P8XM7K/   2.99 
7   TWWETYSEATPROTECTOR   B000P8XM70   http://www.amazon.co.uk/gp/offer-listing/B000P8XM70/   2.99 
8   22436   B000P5QEZU   http://www.amazon.co.uk/gp/offer-listing/B000P5QEZU/   2.99

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/54793-scraping-script-loop-array-query/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.