Yahoo!検索Webサービス ウェブ検索のサンプルコード(VB.NET )

Yahoo!検索Webサービスのウェブ検索機能を使って検索を行うサンプルです。なおこのサンプルを実行するには、xsd.exeにより「/c」オプションで作成されたクラスが必要です。このクラスを添付しておきます。

fileWebSearchResponse.vb 995件 [詳細]
  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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<%@ 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)
        'URLを構築する
        Dim requestUrl As String = _
            "http://api.search.yahoo.co.jp/WebSearchService/V1/webSearch"
        'アプリケーションIDを指定(必ず指定すること)
        requestUrl += "?appid=XXXXX"
        '検索クエリー。UTF-8エンコードする。
        requestUrl += "&query=" + Server.UrlEncode(TextBox1.Text)
        '検索の種類
        If searchType.SelectedValue <> "" AndAlso _
            searchType.SelectedValue <> "all" Then
            requestUrl += "&type=" + searchType.SelectedValue
        End If
        '表示件数
        If resultsCount.SelectedValue <> "" AndAlso _
            resultsCount.SelectedValue <> "10" Then
            requestUrl += "&results=" + resultsCount.SelectedValue
        End If
        '開始位置
        'requestUrl += "&start=" + "1";
        '検索するファイルの種類
        If fileFormat.SelectedValue <> "" AndAlso _
            fileFormat.SelectedValue <> "any" Then
            requestUrl += "&format=" + fileFormat.SelectedValue
        End If
        'アダルトコンテンツを含める
        If adultOk.Checked Then
            requestUrl += "&adult_ok=1"
        End If
        '同じコンテンツを別の検索結果として表示する
        If similarOk.Checked Then
            requestUrl += "&similar_ok=1"
        End If
        '日本語のページのみ検索する(デフォルトでjaとあるのだが...)
        If englishOnly.Checked Then
            requestUrl += "&language=ja"
        End If
        '日本のサイトのみ検索する
        If japanOnly.Checked Then
            requestUrl += "&country=jp"
        End If
        '検索するドメインを指定する
        If searchDomain.Text <> "" Then
            requestUrl += "&site=" + Server.UrlEncode(searchDomain.Text)
        End If
        
        'HttpWebRequestの作成
        Dim request As System.Net.HttpWebRequest = _
            CType(System.Net.WebRequest.Create(requestUrl), _
            System.Net.HttpWebRequest)
        Dim response As System.Net.HttpWebResponse = Nothing
        Dim res As Yahoo.jp.WebSearchResponse.ResultSet
        Try
            'レスポンスの取得
            response = CType(request.GetResponse(), _
                System.Net.HttpWebResponse)
            Dim strm As System.IO.Stream = response.GetResponseStream()
            'サーバーから返されたXMLデータを解析
            Dim serializer As New System.Xml.Serialization.XmlSerializer( _
                GetType(Yahoo.jp.WebSearchResponse.ResultSet))
            res = CType(serializer.Deserialize(strm), _
                Yahoo.jp.WebSearchResponse.ResultSet)
        Catch ex As Exception
            Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message)
            Return
        Finally
            If Not (response Is Nothing) Then
                response.Close()
            End If
        End Try
        
        '結果を表示
        Label1.Text = ""
        Label1.Text += "<b>" + Server.HtmlEncode(TextBox1.Text) + _
            "</b> の検索結果 約 " + res.totalResultsAvailable + _
            "件中 " + res.firstResultPosition + " - " + _
            res.totalResultsReturned + " 件目<br />" + vbLf
        '検索されたページを列挙
        If Not (res.Result Is Nothing) Then
            Label1.Text += "<ul>" + vbLf
            Dim item As Yahoo.jp.WebSearchResponse.ResultType
            For Each item In res.Result
                'リンク
                Label1.Text += "<li><a href=""" + item.ClickUrl + _
                    """>" + item.Title + "</a>"
                '要約
                If Not (item.Summary Is Nothing) AndAlso _
                    item.Summary.Length > 0 Then
                    Label1.Text += "<br />" + item.Summary
                End If 'URL
                Label1.Text += "<br /><small>" + item.Url
                'キャッシュ情報
                If Not (item.Cache Is Nothing) Then
                    Label1.Text += " - " + item.Cache.Size + "byte"
                    Label1.Text += " - <a href=""" + _
                        item.Cache.Url + """>キャッシュ</a>"
                End If
                '更新日
                If Not (item.ModificationDate Is Nothing) AndAlso _
                    item.ModificationDate.Length > 0 Then
                    'UNIXタイムスタンプをDateTimeに変換
                    Dim dt As New DateTime(1970, 1, 1)
                    dt = dt.AddSeconds(Double.Parse(item.ModificationDate))
                    Label1.Text += " - " + dt.ToShortDateString()
                End If
                'MIMEタイプ
                If Not (item.MimeType Is Nothing) AndAlso _
                    item.MimeType.Length > 0 Then
                    Label1.Text += " - " + item.MimeType
                End If
                Label1.Text += "</small>" + ControlChars.Lf
            Next item
            Label1.Text += "</ul>" + ControlChars.Lf
        End If
    End Sub
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Yahoo!ウェブ検索Webサービスのサンプル</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:CheckBoxList ID="searchType" runat="server"
         RepeatDirection="Horizontal"
            RepeatLayout="Flow">
            <asp:ListItem Selected="True" Value="all">すべて含む</asp:ListItem>
            <asp:ListItem Value="any">少なくとも1つ含む</asp:ListItem>
            <asp:ListItem Value="phrase">文章として含む</asp:ListItem>
        </asp:CheckBoxList><br />
        表示件数 :
        <asp:DropDownList ID="resultsCount" runat="server">
            <asp:ListItem Value="10">10件</asp:ListItem>
            <asp:ListItem Value="25">25件</asp:ListItem>
            <asp:ListItem Value="50">50件</asp:ListItem>
        </asp:DropDownList><br />
        ファイル形式 :
        <asp:DropDownList ID="fileFormat" runat="server">
            <asp:ListItem Value="any">すべてのファイル形式</asp:ListItem>
            <asp:ListItem Value="html">HTML (.htm, .html)</asp:ListItem>
            <asp:ListItem Value="pdf">Adobe PDF (.pdf)</asp:ListItem>
            <asp:ListItem Value="xls">Microsoft Excel (.xls)</asp:ListItem>
            <asp:ListItem Value="ppd">Microsoft Power Point (.ppd)</asp:ListItem>
            <asp:ListItem Value="msword">Microsoft Word (.doc)</asp:ListItem>
            <asp:ListItem Value="rss">RSS/XML (.xml)</asp:ListItem>
            <asp:ListItem Value="txt">テキストフォーマット (.txt)</asp:ListItem>
        </asp:DropDownList><br />
        <asp:CheckBox ID="adultOk" runat="server"
         Text="アダルトコンテンツを含める" /><br />
        <asp:CheckBox ID="similarOk" runat="server"
         Text="類似の結果を含める" /><br />
        <asp:CheckBox ID="englishOnly" runat="server"
         Text="日本語のページのみ" /><br />
        <asp:CheckBox ID="japanOnly" runat="server"
         Text="日本にあるウェブサイトのみ" /><br />
        ドメイン :
        <asp:TextBox ID="searchDomain" runat="server">
        </asp:TextBox><br />
        <br />
        <asp:Label ID="Label1" runat="server" EnableViewState="False">
        </asp:Label>
        </div>
    </form>
    
<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet -->
<span style="margin:15px 15px 15px 15px">
<a href="http://developer.yahoo.co.jp/about">Webサービス by Yahoo! JAPAN</a>
</span>
<!-- End Yahoo! JAPAN Web Services Attribution Snippet -->
</body>
</html>

ページ情報
  • 作成日 : 2006-09-09 (土) 02:59:13
  • 作成者 : DOBON!
  • 最終編集日 : 2006-09-09 (土) 03:05:49
  • 最終編集者 : DOBON!
[ トップ ]   [ 編集 | 凍結 | 差分 | バックアップ | 添付 | 複製 | 名前変更 | リロード ]   [ 新規 | 子ページ作成 | 一覧 | 単語検索 | 最終更新 | ヘルプ ]