2014-03-26 51 views
1

我想使用Bing API進行特定於站點的搜索。這裏是我的程序:如何使用Bing API執行特定於站點的搜索?

namespace Microsoft.Samples.BingSearch 
{ class Program 
    { 

     static void Main(string[] args) 
     { 
      string rootUri = "https://api.datamarket.azure.com/Bing/Search"; 
      var bingContainer = new Bing.BingSearchContainer(new Uri(rootUri)); 
      var accountKey = "xxxxxxx"; 
      bingContainer.Credentials = new NetworkCredential(accountKey, accountKey); 
      string query = "SQL Server"; 
      var t = bingContainer.News(query, null, null, null, null, null, null, null, null); 
      t.AddQueryOption("site", "computerworld.com"); 
      var resultNews = t.Execute();  
      foreach(var item in resultNews) 
      { 
       var g = item.ID; 
       Console.WriteLine(item.Title + " - " + item.Source); 
      } 
     } 
    } 
} 

該程序運行(假設你輸入一個有效的帳戶密鑰)並返回結果。不幸的是,它不會將結果限制在computerworld.com。它彷彿線:

 t.AddQueryOption("site", "computerworld.com"); 

被忽略。

我在做什麼錯?

注:我知道我可以通過自己構建URI而不是使用庫來完成特定於站點的搜索。我想知道的是如何正確使用這個庫進行網站特定的搜索。

回答

1

嘗試添加site屬性query本身如下:

var t = bingContainer.News("site:computerworld.com " + query, null, null, null, null, null, null, null, null); 

這種方法是在related ASP.NET blog post概述。

+1

這不起作用,它會生成一個不正確的RequestURI,如下所示: https://api.datamarket.azure.com/Bing/Search/News()?Query='site%3Acomputerworld.comSQL%20Server' – JonnyBoats

+1

@ JonnyBoats這是正確的方法。最初顯示的代碼在字符串連接中缺少一個空格,因此它不能正確生成查詢。 –

+0

@ScottDorman:謝謝你在我吃晚飯的時候背對背。 :} – J0e3gan