如何在 ASP 中使用 Google API






3.50/5 (6投票s)
2003年7月5日
1分钟阅读

93642
本文档展示了如何在 ASP (而非 ASP.NET!) 中使用 Google API。
引言
我已经找到了在 ASP 中使用 Google API 的方法!在使用这些函数之前,请查看 http://api.google.com/。为了使用这些示例,您需要一个密钥,该密钥将限制您每天的查询次数为 1000 次。该密钥可以从 API 网站免费获取。Google API 使用 SOAP 协议,以便在两个不同的操作系统之间轻松传输数据。这些示例使用 Microsoft 的 SOAP 工具包来管理与远程服务器(在本例中为 Google)的连接。 上图是它在我的网站上运行的截图。 这里有一个 搜索功能的完整工作示例。 要使用 API,请在希望显示结果的 ASP 文件中调用这些函数,并指定必要的参数。
要搜索 Google,请使用此函数
GoogleSearch(Query, Start, MaxResults, Filter, Restrict, SafeSurf, Lr)
Dim Key
Key = "your key goes here"
Dim SoapClient
set SoapClient = Server.CreateObject("MSSOAP.SoapClient")
Dim NodeList
SoapClient.ClientProperty("ServerHTTPRequest") = True
SoapClient.mssoapinit "http://api.google.com/GoogleSearch.wsdl"
Set NodeList = SoapClient.doGoogleSearch(key, _
Query, Start, MaxResults, Filter,
Restrict, SafeSurf, Lr, "", "")
Set NodeList = Nothing
Set SoapClient = Nothing
End Function
这将为您提供一个数组,您需要解析该数组才能获取有用的信息。 这里有一个函数,它将以类似于 Google 自身结果的格式格式化结果
Function DisplayList(ByRef ResultNodeList)
ResultNodeList.Reset
Dim documentFiltering, estimatedTotalResultsCount
Dim searchTime, endIndex, searchTips, searchComments
Dim startindex, estimateIsExact, searchQuery
Dim yIndex, tempDir, CategoryHTML, DocSize
Dim DocSnippet, DocCat, DocRelated, DocCatTitle
Dim DocCatDesc, DocURL, DocTitle, iIndex, HTMLOutput
documentFiltering = ResultNodeList.Item(1).Text
estimatedTotalResultsCount = ResultNodeList.Item(3).Text
searchTime = ResultNodeList.Item(7).Text
endIndex = ResultNodeList.Item(11).Text
searchTips = ResultNodeList.Item(13).Text
searchComments = ResultNodeList.Item(15).Text
startindex = ResultNodeList.Item(17).Text
estimateIsExact = ResultNodeList.Item(19).Text
searchQuery = ResultNodeList.Item(21).Text
CategoryHTML = "<H3>Results " & startindex & " - " & endIndex & " of
about " & estimatedTotalResultsCount & ". Search took " & searchTime
& " seconds.</H3>"
For yIndex = 0 To
ResultNodeList.Item(5).childNodes.length - 1
If ResultNodeList.Item(5).childNodes.Item(yIndex).nodeName _
= "item" Then
tempDir = ResultNodeList.Item(5).childNodes.Item(yIndex).Text
CategoryHTML = CategoryHTML & _
"<a href=""http://directory.google.com/" & _
tempDir & "/"">" & DirTree(tempDir) & _
"</a><br>"
Response.Write CategoryHTML
End If
Next
For iIndex = 0 To
ResultNodeList.Item(9).childNodes.length - 1
If ResultNodeList.Item(9).childNodes.Item(iIndex).nodeName = "item" Then
DocSize = _
ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(1).Text
DocSnippet = _
ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(5).Text
DocCat = _
ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(7).Text
DocRelated = _
ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(9).Text
DocCatTitle = _
ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(11).Text
DocCatDesc = _
ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(13).Text
DocURL = _
ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(15).Text
DocTitle = _
ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(17).Text
DocCat = RemoveLB(DocCat)
If DocTitle = "" Then
DocTitle = "Untitled"
End If
If DocSnippet <> "" Then
DocSnippet = DocSnippet & "<br>"
End If
If DocCatDesc <> "" Then
DocCatDesc = "<font color=gray class=sm>Description:</font> " _
& DocCatDesc & "<br>"
End If
If DocCat <> "" Then
DocCat = "<font color=gray class=sm>Category:" & _
" <a href=""http://directory.google.com/" & DocCat & _
"/"">" & DirTree(DocCat) & "</a></font><br>"
End If
HTMLOutput = HTMLOutput & _
"<a href=""" & DocURL & """>" & DocTitle & "</a><br>" & _
DocSnippet & DocCatDesc & DocCat & _
"<font color=green>" & DocURL & " - " & DocSize & _
"</font><br><br>"
End If
Next
Response.Write HTMLOutput
End Function
Function DirTree(tempDir)
Dim tempDirTree, RemoveLB
tempDirTree = Replace(tempDir, "Top/", "")
tempDirTree = Replace(tempDirTree, "/", " > ")
tempDirTree = Replace(tempDirTree, "_", " ")
DirTree = tempDirTree
End Function
Function RemoveLB(in_str)
in_str = Replace(in_str, Chr(10), "")
RemoveLB = in_str
End Function
您可以使用 DisplayList NodeList
来调用它
Google API 还提供拼写检查服务。 这是使用它的函数。
Function PerformGoogleSpellingSuggestion(Words)
Dim Key
Key = "your key goes here"
Dim SoapClient
set SoapClient = Server.CreateObject("MSSOAP.SoapClient")
Dim RetVal
SoapClient.ClientProperty("ServerHTTPRequest") = True
SoapClient.mssoapinit "http://api.google.com/GoogleSearch.wsdl"
RetVal = SoapClient.doSpellingSuggestion(Key, Words)
If RetVal > "" Then
Response.write RetVal
Else
Response.Write "No Suggestions"
End If
Set SoapClient = Nothing
End Function
Google 还提供缓存页面功能,该功能为您提供 Google 访问该页面时的“快照”。 如下所示
Function PerformGoogleGetCachedPage(Url)
Dim Key
Key = "your key goes here"
Dim SoapClient
Set SoapClient = Server.CreateObject("MSSOAP.SoapClient")
Dim RetVal
Dim DecodedPage
Dim i
dim NewArray
SoapClient.ClientProperty("ServerHTTPRequest") = True
SoapClient.mssoapinit "http://api.google.com/GoogleSearch.wsdl"
RetVal = SoapClient.doGetCachedPage(Key, Url)
NewArray = RetVal
for i = 1 to ubound(NewArray)
DecodedPage = DecodedPage & chr(ascB(MidB(NewArray, i, 1)))
next
Response.Write DecodedPage
Set SoapClient = Nothing
End Function
所以,这些都是您可以用来在您的网页上使用的函数。 这些服务完全免费,由 Google 提供。 请注意每天 1000 次的限制!
请查看 我的网站。