O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Yahoo! Hacks
By Paul Bausch
October 2005
More Info

HACK
#71
Program Yahoo! with VBScript
Build Yahoo! searches into Windows programs or ASP pages with VBScript
The Code
[Discuss (0) | Link to this hack]

The Code

Like any other script, the code is simply plain text in a standard text file. You could even use Notepad to save the following code to a file called yahoo_search.vbs:

' yahoo_search.vbs
' Accepts a search term and shows the top results.
' Usage: cscript yahoo_search.vbs <Query> //I
'
' You can create an AppID, and read the full documentation
' for Yahoo! Web Services at http://developer.yahoo.net/

'Set your unique Yahoo! Application ID
Const APP_ID = "insert your app ID"

'Grab the incoming search query or ask for one
If WScript.Arguments.Length = 0 Then
	strQuery = InputBox("Enter a search Term")
Else
	strQuery = WScript.Arguments(0)
End If

'Construct a Yahoo! Search Query
strLanguage = "en"
strReqURL = "http://api.search.yahoo.com/" & _
			"WebSearchService/V1/webSearch?" & _
			"appid=" & APP_ID & _
			"&query=" & strQuery & _
			"&language=" & strLanguage

'Start the XML Parser
Set MSXML = CreateObject("MSXML.DOMDocument")

'Set the XML Parser options
MSXML.Async = False

'Make the Request
strResponse = MSXML.Load(strReqURL)

'Make sure the request loaded
If (strResponse) Then

	'Load the results
	Set Results = MSXML.SelectNodes("//Result")

	'Loop through the results 
For x = 0 to Results.length - 1
	strTitle = Results(x).SelectSingleNode("Title").text
	strSummary = Results(x).SelectSingleNode("Summary").text
	strURL = Results(x).SelectSingleNode("Url").text
	strOut = (x + 1) & ". " & _
			  strTitle & vbCrLf & _
			  strSummary & vbCrLf & _
			  strURL & vbCrLf & vbCrLf
	WScript.Echo strOut
Next

'Unload the results
Set Results = Nothing

End If

'Unload the XML Parser
Set MSXML = Nothing

This code accepts a query on the command line when the script runs, or it asks the user for a query with the InputBox( ) function. From there, the script uses the query to build the proper Yahoo! Web Search request. The results from the request are passed through the Microsoft XML Parser and formatted for display. WScript.Echo sends the results to the user.


O'Reilly Home | Privacy Policy

© 2007 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.