I recently discovered google dropped support for their soap search api and set about finding an alternative solution. There was some initial confusion over the name of their new service because although it's called an ajax api, it's simply a rest web service which returns json and can be consumed by almost anything. Here's an example:
http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Test&start=0
It only returns 4 results per page and 32 results in total (use the start parameter to change the page) which is abit of a pain but it's still better than nothing.
Bing and yahoo have a similar rest api but can return xml which i find alot simpler. However they both require an api key but give you more data than the google service:
http://api.bing.net/xml.aspx?AppId=ApiKeyHere&Sources=Web&Query=Test&Web.Count=10&Web.Offset=0
http://boss.yahooapis.com/ysearch/web/v1/Test?appid=ApiKeyHere&format=xml&start=0
Here's a basic implementation using linq to xml to parse the bing results and return the position of the key phrase "Test" for the site http://www.yoursite.com:
int position = -1;
int maximumResults = 1000;
int numPerPage = 10;
int counter = 1;
bool isFound = false;
// Need to loop through the pages
for (int i = 0; i < maximumResults / numPerPage; i++)
{
if (!isFound)
{
try
{
var bingResults = XDocument.Load(string.Format("http://api.bing.net/xml.aspx?AppId=ApiKeyHere&Sources=Web&Query=Test&Web.Count=10&Web.Offset={0}", i * numPerPage));
XNamespace bns = "http://schemas.microsoft.com/LiveSearch/2008/04/XML/web";
var data = (from item in bingResults.Root.Element(bns + "Web").Element(bns + "Results").Elements(bns + "WebResult")
select new
{
Title = item.Element(bns + "Title").Value,
Description = item.Element(bns + "Description") != null ? item.Element(bns + "Description").Value : null,
Url = item.Element(bns + "Url").Value
}).ToList();
foreach (var result in data)
{
isFound = result.Url.ToLower().Contains("http://www.yoursite.com");
if (isFound)
{
position = counter;
break;
}
counter += 1;
}
}
catch { }
}
}
Using the same logic as above here's how you parse the results from google (using linq to json see
http://json.codeplex.com/):
data = (from item in JObject.Parse(googleResults)["responseData"]["results"].Children()
select new SearchResult
{
Title = item.Value<string>("title"),
Description = item.Value<string>("content"),
Url = item.Value<string>("url")
}).ToList();
Note: googleResults is simply a string with the json data in and can be returned by using HttpWebRequest.
Hope this helps.
Added By:
Lee Timmins on 10th Jul 2009 at 11:21
Last Updated: 30th Jul 2009 at 23:33