Google SOAP Search API - Web検索機能のサンプル(VB.NET) †
ここではコードのみを示します。詳しい解説は、C#のサンプルをご覧ください。
コード †
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
| | <%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim key As String = "XXXXXXXXXXXXXXXXXXXXXX"
Dim startIndex As Integer = 0
Dim maxResults As Integer = 10
Dim lr As String = ""
If langJapanese.Checked Then
lr = "lang_ja"
End If Dim restricts As String = ""
If countryJapan.Checked Then
restricts = "countryJP"
End If Dim inputEncoding As String = ""
Dim outputEncoding As String = ""
Dim gs As New Google.GoogleSearchService()
Dim res As Google.GoogleSearchResult = Nothing
Try
res = gs.doGoogleSearch( _
key, _
TextBox1.Text, _
startIndex, _
maxResults, _
autoFiltering.Checked, _
restricts, _
safeSearch.Checked, _
lr, _
inputEncoding, _
outputEncoding)
Catch ex As System.Web.Services.Protocols.SoapException
Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message)
Return
End Try
If res Is Nothing Then
Label1.Text = "エラー:結果を取得できませんでした。"
Return
End If
Label1.Text = ""
Label1.Text += "<b>" + Server.HtmlEncode(res.searchQuery) + _
"</b> の検索結果 約 " + _
res.estimatedTotalResultsCount.ToString() + "件中 " + _
res.startIndex.ToString() + " - " + _
res.endIndex.ToString() + " 件目 (" + _
res.searchTime.ToString() + " 秒)<br />"
If Not (res.searchComments Is Nothing) AndAlso _
res.searchComments.Length > 0 Then
Label1.Text += "<br />" + res.searchComments + "<br />"
End If If Not (res.searchTips Is Nothing) AndAlso _
res.searchTips.Length > 0 Then
Label1.Text += "<br />" + res.searchTips + "<br />"
End If
If Not (res.directoryCategories Is Nothing) AndAlso _
res.directoryCategories.Length > 0 Then
Label1.Text += "<br /><b>カテゴリ:</b><br />"
Label1.Text += "<ul>"
Dim categories As Google.DirectoryCategory
For Each categories In res.directoryCategories
Label1.Text += "<li>" + categories.fullViewableName
Next categories
Label1.Text += "</ul>"
Label1.Text += "<br />"
End If
If Not (res.resultElements Is Nothing) AndAlso _
res.resultElements.Length > 0 Then
Label1.Text += "<ul>"
Dim element As Google.ResultElement
For Each element In res.resultElements
Label1.Text += "<li><a href=""" + element.URL + """>"
If Not (element.title Is Nothing) AndAlso _
element.title.Length > 0 Then
Label1.Text += element.title
Else
Label1.Text += element.URL
End If
Label1.Text += "</a>"
If Not (element.snippet Is Nothing) AndAlso _
element.snippet.Length > 0 Then
Label1.Text += "<br />" + element.snippet
End If
Label1.Text += "<br /><small>" + element.URL
Label1.Text += " - " + element.cachedSize
If element.relatedInformationPresent Then
Label1.Text += " - 関連ページあり"
End If
Label1.Text += "</small>" + ControlChars.Lf
Next element
Label1.Text += "</ul>"
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Google SOAP Search APIのサンプル</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="検索開始" /><br />
<asp:CheckBox ID="autoFiltering" runat="server"
Text="類似の結果を隠す" /><br />
<asp:CheckBox ID="langJapanese" runat="server"
Text="日本語のページのみ検索する" /><br />
<asp:CheckBox ID="countryJapan" runat="server"
Text="日本のサイトのみ検索する" /><br />
<asp:CheckBox ID="safeSearch" runat="server"
Text="アダルトコンテンツを除外する" /><br />
<br />
<asp:Label ID="Label1" runat="server" EnableViewState="False">
</asp:Label></div>
</form>
</body>
</html>
|
|