スポンサード リンク

2008年06月14日

Yahoo WebサービスからXMLを取得する

Visual Basic 2008(vb2008)を使って、Yahooの関連検索ワードWebサービスから、XMLを引っ張ってこようと思います

Yahooの関連検索ワードWebサービス

YahooにあるサンプルのXMLを拾うURLは
サンプルリクエストURL
http://api.search.yahoo.co.jp/AssistSearchService/V1/webunitSearch?appid=YahooDemo&query=%e6%b2%96%e7%b8%84&results=2
そうすると、xmlで沖縄 の関連キーワードが出力されます

以下のファンクションは
Keyword に検索したいKeyword

Countに出力してほしい件数 URL のresults=2 の2の部分)をいれて
URLAssistSearchURL(Keyword, Count)
でリクエストのURLを作成して

XDocument.Load(YahooAPI.URLAssistSearchURL(Keyword, Count))

でXMLを取得しています。

そして検索結果をList形式で

return しています。

Option Strict On
Imports System.Xml.Linq
Imports System.Xml
Imports System.Net
Imports System.Web
Imports System
Imports System.IO
Imports System.Text
Imports System.Threading.Thread
Imports
Imports

Public Function YahooAssistSearch(ByVal Keyword As String, ByVal Count As String) As List(Of String)
Dim YahooXML As XDocument
Dim aw As XNamespace = "urn:yahoo:jp:srchunit"
Dim ICount As Long
Dim List As New List(Of String)
'Yahoo APIからXMLを取得

YahooXML = XDocument.Load(YahooAPI.URLAssistSearchURL(Keyword, Count))
'Clipboard.SetText(YahooAPI.URLAssistSearchURL(Keyword, Count))
'XMLからtotalResultsAvairable 何件ヒットしたかを取得して、Clngでlong型に変更する
ICount = CLng(YahooXML.Elements(aw + "ResultSet").@totalResultsAvailable.ToString)
Dim query As IEnumerable(Of XElement) = YahooXML.Elements(aw + "ResultSet").

'結果があれば
'If ICount > 0 Then
'Keyword 自体も追加
List.Add(Keyword)
For Each result In query
List.Add(result.Value)
Next
'End If

Return List

End Function
posted by kiyo at 20:46| Comment(0) | TrackBack(0) | xml | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

この記事へのトラックバックURL
http://blog.seesaa.jp/tb/100520282

この記事へのトラックバック