Friday April 19, 2024

Basic eBay Search Parsing

Finding the search results
Now that we have tables seperated into array elements, we need to figure out which table contains the search result data. We can do this by searching for words, a picture, some kind of unique identifier around the data we need to grab. In the case of eBay's pages at the time of this article, I found that "Customize Display" occured right before the search results list started.

<?
$URL 
"http://search.ebay.com/search/search.dll?query=".$searchstring."&sosortproperty=1&ht=1&from=R10&BasicSearch=";
$file fopen("$URL""r");
$r "";
do{
    
$data fread($file8192);
    
$r .= $data;
}
while(
strlen($data) != 0);

$ebayTABLEArray preg_split ("/<table.*?>/"$r);

// Now try to find which <table> contains the search results are
for($x=0$x<count($ebayTABLEArray); $x++){
    
// text that occurs right before search results
    
if(strstr($ebayTABLEArray[$x],"Customize Display")){
        
$resultTable $x+1;
    }
}
?>

Explanation:

  • Search for the table containing the words "Customize Display", which help us find where the search results begin.

Second verse, same as the first..
Now that we know what table contains "Customize Display" (table number was stored in $resultTable), we can use the same technique for seperating the individual table elements into an array. We can then use some regular expressions to grab the specific titles, price, item number, etc out of the data.

<?
$URL 
"http://search.ebay.com/search/search.dll?query=".$searchstring."&sosortproperty=1&ht=1&from=R10&BasicSearch=";
$file fopen("$URL""r");
$r "";
do{
    
$data fread($file8192);
    
$r .= $data;
}
while(
strlen($data) != 0);

$ebayTABLEArray preg_split ("/<table.*?>/"$r);

// now try to find which <table> contains the search results are
for($x=0$x<count($ebayTABLEArray); $x++){
    if(
strstr($ebayTABLEArray[$x],"Customize Display")){
        
$resultTable $x+1;
    }
}

$ebayTRArray preg_split("/<tr.*?>/",$ebayTABLEArray[$resultTable]);

echo 
"<BR><B>Ebay Results:</B><BR><BR>";
$start=2;
$end $start count($ebayTRArray);

for(
$i=$start;$i<$end;$i++){
    
$ebayTDArray preg_split ("/<td.*?>/",$ebayTRArray[$i]);
    
//print_r($ebayTDArray);
    
preg_match("/<a.*?<\/a>/",$ebayTDArray[4],$match);
    
$item strip_tags($match[0],"<A></A>");
    
    if(
$item!=""){
        
$item_name $item;
        
$price strip_tags($ebayTDArray[6]);
        
// see if there's a Buy-It-Now price
        
$priceArray explode("$",$price);
        
$item_price $priceArray[1];
        
$item_bin $priceArray[1];
        
$item_bids strip_tags($ebayTDArray[7]);
        
$item_timeleft strip_tags($ebayTDArray[8]);

        echo 
$item_name." ".$item_price." ".$item_bids." ".$item_timeleft."<BR>";
    }
}
?>

Explanation:

  • Seperate on <tr> tags to place item data into seperate array elements
  • Use regular expressions to seperate item data into useable variables.
page1 page2 page3


Comments:
Anonymous Techdoser
Posted 01/18/13 2:37PM by Anonymous Techdoser
Possibly out of date for the new ebay, we will see. However, this is the BEST explanation of preg_split I have ever seen and I can see it's potential for many things. Many thanks
Re: Completed listings
Posted 03/23/08 11:23AM by AceBHound
I don't have specifics on how to pass the password credentials to a site, but I know it's possible because there are programs and applications out there that are capable of doing this.
Completed listings?
Posted 12/24/06 3:26AM by Anonymous Techdoser
Is it possible to search through completed listings? So, is it possible to log-in (without the visitor of my site knowing the log-in/password) and extract these auctions as well?
Thanks!
Posted 04/23/06 12:27AM by Anonymous Techdoser
Thanks for writing this article. I appreciate all the work you've put into it and wanted to say IT WORKS! :O)

Best article with a working example I have ever found online!