In previous post, the implementation of Google book search in C# is discussed. In this post, the similar approach is used to implement Google Custom Search in C#.
Download the source project at Github here.
The code is very simple:
public static IList<Result> Search(string query) { Console.WriteLine("Executing custom search for query: {0} ...", query); CseResource.ListRequest listRequest = Service.Cse.List(query); listRequest.Cx = cx; Search search = listRequest.Execute(); return search.Items; }
To get this code running, we need an instance variable called Service:
//API Key private static string API_KEY = "AIzaSyCsqAm43dcaVHD6ycUEWTbCNtg4rD_aa8Y"; //The custom search engine identifier private static string cx = "015598178761323117960:sbbkk2__0lo"; public static CustomsearchService Service = new CustomsearchService( new BaseClientService.Initializer { ApplicationName = "ISBNBookCsutomSearch", ApiKey = API_KEY, });
To get this code compile, you need to add reference to the Google Custom Search Library:
- You can use Nuget command: Install-Package Google.Apis.Customsearch.v1 or
- Right click on project reference > Manage Nuget Packages > Search “Google.Apis.Customsearch.v1” in the top right corner > Install the found package
Compile and Run the below test code:
class Program { static void Main(string[] args) { string query = "The University of Hong Kong"; var results = GoogleSearch.Search(query); foreach (Result result in results) { Console.WriteLine("-----------------------------------"); Console.WriteLine("Title: {0}", result.Title); Console.WriteLine("Link: {0}", result.Link); } Console.ReadKey(); } }
You should have a success in running Google search of your custom keyword!
Again, you might wonder what are the API KEY and cx? How to get these two strings?
- Goto Google Developer Console
- Create a project
- Click “Credentials” on the left Tab
- Click “Create new key” button on the right panel
-
To get the Custom Search engine ID (cx), follow below procedures:
-
If you didn’t create a custom search engine yet, you can create one via Custom Search control panel
-
You can find your Custom Search engine ID (cx) on the Basics tab of the Custom Search control panel.
- One last step: turn on the custom search api in your development console:
As you can see, the code is trivial, and the complexity is mostly due to Google configurations!
Happy coding and the code project can be found at Github here.
Hi there,
thanks for the splendid tutorial and the example code, it helped me a lot.
Now I run a search following your example and when I compare the number of results I get via the API to the number of results I get via the Google website, the API returns significantly less results.
Do you have any idea how that could come?
The custom search is different from your google search via web, it is normal. If you want exactly the same results as you do in browser, you can use HttpAgility Pack to query at runtime!
Hi! Thanks for the tutorial 🙂
Just a question, how can I order more results for given query? Is there any chance to print all the results till the “no more results” flag?
Cheers,
Tom
var results = GoogleSearch.Search(query); this returns IEnumerable, you can use LINQ to sort the results by any order. e.g. var results2 = results.Where (item => item.SomeProperty == “abc”);
Thanks for the previous reply!
How about using other CSE parameters?
https://developers.google.com/custom-search/json-api/v1/reference/cse/list
I wonder where should I edit code to make use of parameters like start or num. Also, how to get exact number of results? I’m beginner in the topic, so please be patient with my questions 😉
Hi! I tried your code and modify it a little as I am using MVC.
List search_list = new List();
foreach (var item in search.Items)
{
var list = new SearchList();
list.AdLink = item.Link;
list.AdTitle = item.Title;
list.AdDetails = item.Snippet;
list.AdOptions = item.Labels;
search_list.Add(list);
}
But then in my view page, it says that my search_list is NullReferenceException.
@if (Model.search_list.Count != 0)
{
@foreach (var item in Model.search_list)
{
@item.AdLink
@item.AdTitle
@item.AdDetails
}
}
Please help me.
Pingback: Bing Search in C#: A step by step walk-through tutorial | Xinyustudio
thanks very good