Contents
Instructions
Create an ASP page, inserting the ASP code below at the very
start of the page. You will need to create a database table to hold the
results (see below) and enter a valid connection string in the variable
indicated.
Insert the HTML code below between the <body> tags of
your ASP page. It simply outputs the data extracted to the page so you
can check it is working as it should.
Use the SQL code to create queries in your database which aggregate
the data you have extracted.
Please note the following:
- No warranties or guarantees are offered for this code. It has been
tested and used by us, but your system settings, eBay changes, and
other factors may stop it working as it should.
- You use this code at your own risk. We are not responsible for any
damage or complaint arising from its use.
- You may freely use and adapt this code for any purpose, as you see
fit. Please credit this site if you do so.
ASP Code
Dim strDBConnection, objXMLHttp, strURL, strResponse, reAllCats
Dim reLevel1Cats, reLevel2Cats, Match, Matches, strOutPut
Dim strTopLevelCat, strSQL, con, strCatName
strDBConnection = "ENTER YOUR CONNECTION STRING HERE"
Set objXMLHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
strURL = "http://pages.ebay.com/buy/index.html?ssPageName=HAC01"
objXMLHttp.Open "GET", strURL, False
objXMLHttp.Send
strResponse = objXMLHttp.ResponseText
Set objXMLHttp = Nothing
Set reLevel1Cats = New RegExp
reLevel1Cats.Global = True
reLevel1Cats.IgnoreCase = True
reLevel1Cats.Pattern = "<a href=""http:\/\/page" & _
"s\.ebay\.com\/\w{1,20}\-index\.html\?from=R12""" & _
"><b>([^<]*)<\/a>"
Set reLevel2Cats = New RegExp
reLevel2Cats.Global = True
reLevel2Cats.IgnoreCase = True
reLevel2Cats.Pattern = "<a href=""http:\/\/listings\.ebay\." & _
"com\/aw\/plistings\/list\/category(\d{2,5})\/index\.html" & _
"\?from=R12"">([^<]*)<\/a><\/FONT><font size = ""1""> \((" & _
"\d{1,10})\)" & _
Set reAllCats = New RegExp
reAllCats.Global = True
reAllCats.IgnoreCase = True
reAllCats.Pattern = reLevel1Cats.Pattern & "|" & reLevel2Cats.Pattern
Set con = Server.CreateObject("ADODB.Connection")
con.Open strDBConnection
Set Matches = reAllCats.Execute(strResponse)
For Each Match in Matches
If reLevel2Cats.Test(Match.Value) = True Then
strOutput = strOutPut & reLevel2Cats.Replace(Match.Value, & _
"<tr> <td>$1</td> <td><img src='images/spacer.gif' " & _
"height='1' width='10'></td> <td>$2</td> <td>$3</td" & _
"> <td>items</td> </tr>" & vbNewLine)
strCatName = reLevel2Cats.Replace(Match.Value, "$2")
strCatName = Replace(strCatName, "'", "''")
strSQL = reLevel2Cats.Replace(Match.Value, "INSERT INTO " & _
"tblEbayCategories (CatNumber, TopLevelName, CatName, " & _
"ItemCount) VALUES ($1,'" & _
Replace(strTopLevelCat, "'", "''") & "','" & _
strCatName &"',$3);") & vbNewLine
con.Execute strSQL
Else:
strTopLevelCat = reLevel1Cats.Replace(Match.Value, "$1")
strOutput = strOutput & "<tr><td colspan='5'> " & _
"</td></tr><tr><td colspan='3'><h2>" & strTopLevelCat & _
"</h2></td></tr>" & vbNewLine
End If
Next
con.Close
Set con = Nothing
%>
HTML Code
<%Response.Write("<table>" & strOutPut & "</table>")%>
SQL Code
This counts the number of items in each first-level category:
SELECT TopLevelName, SUM(ItemCount) AS TotalItems
FROM tblEbayCategories
GROUP BY TopLevelName
ORDER BY SUM(ItemCount) DESC
This returns the top 20 most popular second-level categories:
SELECT TOP 20 TopLevelName, CatName, SUM(ItemCount) AS TotalItems
FROM tblEbayCategories
GROUP BY CatName, TopLevelName
ORDER BY SUM(ItemCount) DESC
|