Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC
Article

CGoogle: A Google Search class

Rate me:
Please Sign up or sign in to vote.
4.84/5 (27 votes)
8 Dec 20053 min read 150.6K   2.3K   81   39
A simple class to perform Google searches without the official Google API.

Introduction

This article provides an easy to use alternative to querying Google via your program through the Google API and SOAP. Many people don't wish to take the time to download the official Google API package and integrate it into their project, so this simple class makes querying Google for search results child's play.

Before You Begin

Before you jump into this article and code, there are several things I'd like to address. I'll keep the brevity of each at a non-intrusive level.

#1: The most important

Google is a wonderful service provided to us free of charge, please respect this and refrain from abusing Google. Try to keep your queries minimal, don't bombard Google's servers with thousands of needless requests.

#2: Susceptible to change

Google definitely isn't immune to change. If Google heavily modifies the format in which results are returned, this class may fail to retrieve the proper results. Such a possibility is very rare in the short-term future, but if it does occur, I'll update CGoogle to comply with any new formatting standards.

#3: A note about MFC

I'm certain there are many of you weeping (I can dream, can't I?) as you learn this class is MFC dependent. But don't runaway, there is good news! Very shortly, I'll provide a non-MFC class version, which will be my personal preference over the MFC dependency.

Using the code

In the past, I've been criticized for publishing articles which are needlessly long, so I'll attempt to restrain myself on this article. Particularly because there isn't much to explain, using Google is simple, so shouldn't accessing it with your program be simple as well? My opinion revolves around an obvious yes.

Searching Google

Throughout this class are six different public access functions, but most likely you'll only use four of them. I'll start with the most important function first.

If and when you're ready to search Google, you employ PerformSearch to do the work for you. The function takes only one parameter, which is a character array containing your search term(s). PerformSearch returns true on success and false on failure.

You can enable or disable your search to be parentally safe with SetSafeSearch. By default, safe search is disabled.

Getting the Result Count

When you are ready to retrieve the results, you'll need to know how many results were found so you do not unintentionally step out of bounds. Calling GetResultCount will clearly return the number of results found.

Retrieving Results

After you've called PerformSearch, you'll surely want to retrieve individual results for further processing, you may do so with either the subscript operator or by using GetAt.

GetAt will return a pointer to the result located at the given zero-based index; if the result does not exist at the specified position, NULL is returned.

The subscript operator will return a reference to the corresponding result, however, it isn't bounds safe (meaning it will fail if the passed index is less than zero or greater than or equal to GetResultCount()).

The base return type shared by both is GoogleResult, which is a structure conveniently containing the result's URL (cstrURL), title (cstrTitle), and description (cstrDesc).

But what is an article without examples? Certainly not appealing? For those of you that may find yourselves in a predicament when it comes to using this class, perhaps this keen example will provide itself to be a useful reference:

/* The following will search google for "The Code Project"
 and display the first 3 results in message boxes */


bool GoogleTest()
{
    CGoogle google;

    if  (!google.PerformSearch( "\"The Code Project\"" ))
    {
        return false;  // Failed
    }

    size_t nResultCount = google.GetResultCount();

    for ( size_t n=0; n < nResultCount && n < 3; ++n)
    {
        ::MessageBox(  NULL, google[n].cstrTitle,  "Result Title",
                                             MB_ICONINFORMATION );

        ::MessageBox(  NULL, google[n].cstrURL,  "Result URL",
                                         MB_ICONINFORMATION );

        ::MessageBox(  NULL, google[n].cstrDesc,  "Result Description",
                                                  MB_ICONINFORMATION );
    }

    return true;
}

History

  • March 23, 2005 - Began work on CGoogle.
  • March 24, 2005 - Submitted CGoogle to The Code Project.
  • April 06, 2005 - Fixed minor bounds bug.
  • April 10, 2005 - Fixed bug affecting description-less results.
  • November 7, 2005 - Updated code to be compatible with new Google search results.
  • November 29, 2005 - Updated code to be compatible with new Google search results.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Well first of all, it is fairly obvious my name is Stuart Konen (I'm sure 50% of you just took notice), all of my life I've lived on a farm located in Northern Idaho. What shatters the stereotype of rural residence however, is the fact that I'm very active in the technology and programming worlds. I took up the hobby of programming at age 9, at that point it was little language known as Quick Basic *sigh*. Fast forward another 9 years... (Woah... I just realized that's half of my existence. But that's something I'll have to contemplate later, as I have an autobiography to tell).

Now my experience in programming has improved vastly, I've released various technologies and programs and I'm continuing to pump out concepts and systems that are getting glances from all over the world. My weapon of choice is C++, the core language of the majority of freshly released software, it's sleak, mean, and incrediably powerful. On the side I venture into web application development and site design, where my interest lies in PHP. Over the years my project have included everything from Artificial Intelligence to Web Statistic Tracking systems, but that's the past. What matters is the future... Remember that question we were always asked in grade school? Where did we see ourselves in 10 years. Well that question was asked about 8 years ago in my life, so it looks as though I only have two more for planning stages. In two years I see myself plunging into the world of research, creating my own Research and Development firm, aiming to meet the never-ending need for new and superior software and technology. Soon after becoming a multi-billionare I'll pursue my dream of world domination. Nobody is perfect...

Actually when it comes down to things, the money has no meaning. But there you have it, a 5 minute slice of my thoughts and time... If you have any job opportunities or have the slight urge to initiate a conversation with me, it can be done via email: skonen [at] gmail [dot] com

Comments and Discussions

 
QuestionHow Google Adsence script work see this example Pin
isonline28-Jun-12 10:16
isonline28-Jun-12 10:16 
Questionnon mfc? Pin
petter10010-May-08 1:04
petter10010-May-08 1:04 
NewsGoogle Parser online tool (GooParser) Pin
jp73129-Oct-07 17:21
jp73129-Oct-07 17:21 
GeneralIt doesn't work! Pin
Sergey Kolomenkin3-Sep-07 0:48
Sergey Kolomenkin3-Sep-07 0:48 
GeneralGoogle Retaliating Pin
Stuart Konen28-Nov-05 12:58
Stuart Konen28-Nov-05 12:58 
GeneralUpdate Sent Pin
Stuart Konen28-Nov-05 13:12
Stuart Konen28-Nov-05 13:12 
GeneralRe: Update Sent Pin
Teashirt28-Dec-05 18:17
Teashirt28-Dec-05 18:17 
GeneralRe: Google Retaliating Pin
Ravi Bhavnani9-Dec-05 1:53
professionalRavi Bhavnani9-Dec-05 1:53 
GeneralNice. This goes around the 1000 query per day per licence limit Pin
C-codist13-Dec-05 19:05
C-codist13-Dec-05 19:05 
GeneralGood Code...would have been nice if it worked Pin
BobbyQSoft28-Nov-05 10:15
BobbyQSoft28-Nov-05 10:15 
GeneralCGoogle does't work well. Pin
MyGoodCode26-Nov-05 17:16
MyGoodCode26-Nov-05 17:16 
GeneralRe: CGoogle does't work well. Pin
sledge14-Dec-05 9:52
sledge14-Dec-05 9:52 
GeneralRe: CGoogle does't work well. Pin
Stuart Konen4-Dec-05 10:16
Stuart Konen4-Dec-05 10:16 
Generalmethod: HTML scraping Pin
Ben Bryant23-Nov-05 18:13
Ben Bryant23-Nov-05 18:13 
It would have saved me a little time if you had mentioned somehwre what your method is. Briefly, it looks like you are actually running the search in HTML and scraping the results out of the HTML source. I was wondering if you were using an XML API (other than the SOAP one). Thanks.
GeneralNeedlessly short Pin
Hans Dietrich23-Nov-05 15:01
mentorHans Dietrich23-Nov-05 15:01 
General&quot;403 Forbidden&quot; page Pin
choecm5556-Nov-05 21:49
choecm5556-Nov-05 21:49 
GeneralRe: &amp;quot;403 Forbidden&amp;quot; page Pin
Stuart Konen7-Nov-05 19:59
Stuart Konen7-Nov-05 19:59 
GeneralMining Google Web Services: Building Applications with the Google API Pin
Anonymous21-Aug-05 20:45
Anonymous21-Aug-05 20:45 
QuestionVery nice, any news on the non MFC ver? Pin
Avis p10-Aug-05 10:42
Avis p10-Aug-05 10:42 
QuestionWhat's wrong??? Pin
Tcpip200519-Jun-05 17:18
Tcpip200519-Jun-05 17:18 
AnswerRe: What's wrong??? Pin
Stuart Konen29-Jun-05 7:23
Stuart Konen29-Jun-05 7:23 
GeneralSlight Problem Pin
ettt618-Apr-05 10:05
ettt618-Apr-05 10:05 
GeneralIt's very easy Pin
Stuart Konen18-Apr-05 12:52
Stuart Konen18-Apr-05 12:52 
GeneralRe: It's very easy Pin
ettt618-Apr-05 13:56
ettt618-Apr-05 13:56 
GeneralRe: It's very easy Pin
Stuart Konen18-Apr-05 18:44
Stuart Konen18-Apr-05 18:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.