投稿数 - 112, コメント - 212, トラックバック - 17

YAHOO検索エンジンを利用してみた

Yahooもgoogleに負けじと検索エンジンを公開したけど、

使い物になるのか、ちょと試してみた。

ちなみに、詳細はこちらを参照してみてください。

(サンプルの一部なので参考にする程度で・・・)

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        Dim strURL As String = "http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo"
        Dim xmlTextReader As XmlTextReader

        If (txtSearchValue.Text.Length = 0) Then
            MessageBox.Show("検索文字列を入力してください。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        'URL作成
        strURL = strURL & "&start=1"
        strURL = strURL & "&results=" & cmbDisp.Text
        strURL = strURL & "&language=" & cmbLanguage.Text.Substring(0, 2)
        strURL = strURL & "&query=" & fncGetUrlEncode(txtSearchValue.Text)

        'URL読み込み
        xmlTextReader = New XmlTextReader(strURL)

        'リストビュークリアー
        Call lvwList.Items.Clear()

        '空白は無視する
        xmlTextReader.WhitespaceHandling = WhitespaceHandling.None

        Try
            Cursor.Current = Cursors.WaitCursor
            'XML読み込み
            While (xmlTextReader.Read)

                If (xmlTextReader.NodeType = XmlNodeType.Element) Then
                    If (xmlTextReader.Name = "ResultSet") Then
                        Trace.WriteLine(">>" & xmlTextReader.GetAttribute("totalResultsAvailable"))
                    End If
                    If (xmlTextReader.Name = "Result") Then
                        xmlTextReader.Read()
                        Dim lvwItem As ListViewItem
                        Dim ResultTitle As String = xmlTextReader.ReadElementString("Title")
                        Dim ResultSummary As String = xmlTextReader.ReadElementString("Summary")
                        Dim ResultUrl As String = xmlTextReader.ReadElementString("Url")
                        Dim ResultClickUrl As String = xmlTextReader.ReadElementString("ClickUrl")
                        Dim ResulModificationDate As Date = fncGetTimeUTC(xmlTextReader.ReadElementString("ModificationDate")).Date

                        ' リストビューアイテム生成
                        lvwItem = New ListViewItem(New String() {ResultTitle, _
                                                                 ResultSummary, _
                                                                 ResulModificationDate, _
                                                                 ResultUrl})
                        ' リストビューアイテム追加
                        lvwList.Items.AddRange(New ListViewItem() {lvwItem})

                    End If
                End If
            End While
        Catch ex As Exception
            MessageBox.Show(ex.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Cursor.Current = Cursors.Default
        End Try

    End Sub

YAHOO検索エンジンを利用してみる(7KB)

投稿日時 : 2005年3月28日 21:08

フィードバック

No comments posted yet.

コメントの投稿

タイトル  
名前  
URL
コメント