The Code
Create a text file
called
amazon_price.vbs
with the following code:
' amazon_price.vbs
' This VBScript prompts for an ASIN, and returns the price.
' Usage: double-click amazon_price.vbs, enter ASIN.
Sub AmazonQuery(ASIN_In)
Dim SelectedText
Dim MSXML
Dim XMLURL
Dim Loaded
' Make sure that some text is selected
If ((Len(ASIN_In) = 0) Or (ASIN_In < " ")) Then
WScript.Echo "Please enter an ASIN."
Exit Sub
End If
' Set Associate ID and Developer Token
AffiliateTag = "insert associate tag"
DeveloperToken = "insert developer token"
' Create an instance of the MSXML Parser
Set MSXML = CreateObject("MSXML.DOMDocument")
' Set MSXML Options
MSXML.Async = False
MSXML.preserveWhiteSpace = False
MSXML.validateOnParse = True
MSXML.resolveExternals = False
' Form the request URL
XMLURL = "http://xml.amazon.com/onca/xml3" + _
"?t=" + AffiliateTag + _
"&dev-t=" + DeveloperToken + _
"&page=1" + _
"&f=xml" + _
"&mode=books" + _
"&type=lite" + _
"&AsinSearch=" + ASIN_In
' Issue the request and wait for the response
Loaded = MSXML.Load(XMLURL)
' If the request is loaded successfully, continue
If (Loaded) Then
' Look for the ErrorMsg tag
Set XMLError = MSXML.SelectNodes("//ErrorMsg")
' If it exists, display the message and exit
If XMLError.length > 0 Then
WScript.Echo MSXML.SelectSingleNode("//ErrorMsg").text
Exit Sub
End If
' If there's no error, use XPath to get the product name
' and the price
WScript.Echo "The price of " + _
MSXML.SelectSingleNode("ProductInfo/Details/ProductName").text + _
" is " + _
MSXML.SelectSingleNode("ProductInfo/Details/OurPrice").text
Else
WScript.Echo "The service is not available."
End If
End Sub
strASIN = InputBox("Please enter a product ASIN.")
AmazonQuery(strASIN)
As you can see, this script looks for two specific pieces of
information in the response: the product name and the price. The
XPath queries to reach those pieces of info are:
ProductInfo/Details/ProductName
ProductInfo/Details/OurPrice
By changing these paths, you can bring up any other bits of available
data.