O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
  Books     O'Reilly Gear     Newsletters     Press Room     Jobs     About O'Reilly  

 
• List of Titles
• Got a Hack?
• Suggestion Box
• About Hacks
Bioinformatics
C/C++
Databases
Digital Media
Enterprise Development
Game Development
Java
Linux/Unix
Macintosh/OS X
.NET
Open Source
Oracle
Perl
Python
Scripting
Security
SysAdmin/Networking
Web
Web Services
Windows
Wireless
XML
CD Bookshelves
Cookbooks
Developer's Notebooks
Hacks
Head First
In A Nutshell
Missing Manuals
Pocket References
Technology & Society
No Starch Press
Paraglyph Press
Pragmatic Bookshelf
SitePoint
Syngress Publishing
LinuxDevCenter.com
MacDevCenter.com
ONDotnet.com
ONJava.com
ONLamp.com
OpenP2P.com
Perl.com
WebServices.XML.com
WindowsDevCenter.com
XML.com
Ask Tim
Beta Chapters
Events
From the Editors List
Letters
MAKE
Open Books
tim.oreilly.com
Academic
Corporate Services
Government
About O'Reilly
Bookstores
Catalog Request
Contact Us
International
User Groups
Writing for O'Reilly
Traveling to
a tech show?

Lubbock Hotels
Kihei Hotels
Ann Arbor Hotels
Fargo Hotels
Florence Hotels
Fredericksburg Hotels
Santa Cruz Hotels
Englewood Hotels



 
Buy the book!
Amazon Hacks
By Paul Bausch
August 2003
More Info

HACK
#82
Program XML/HTTP with VBScript
Accessing Amazon within Windows is a job for VBScript
The Code
[Discuss (0) | Link to this hack]

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.


O'Reilly Home | Privacy Policy

© 2004, O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.