"Improve your... Google?"

While developing the code analyzer PVS-Studio intended for searching issues in 64-bit and concurrent software, we came to the need of collecting fresh information on the Internet on some topics. For example, it is always useful to answer the questions of programmers who may be interested in our tool on various forums and blogs. While collecting the data we found out that there is much information on the Internet and therefore manual search might be very long and tiresome. Thus the task of automating the process of searching for fresh data appeared. In this post we will tell you how we do this.
But I bet you have said right now: "Ha-ha! The guys are reinventing the wheel and are not aware of Google Alerts". Well, we are aware of Google Alerts. And it is almost the thing we need but not quite :-). We have been using Google Alerts for more than half a year and did not manage to get what we needed. And here is what we need:
  • search on some particular listed sites;
  • search over the period of only the last twenty-four hours;
  • capability to add stop words;
  • Google Alerts provides some mechanism of additional filtration of results. I.e. the usual Google search is more helpful than Google Alerts.
That is why we decided to reinvent the wheel.
Within the scope of this task we need to implement the search of new materials on particular sites - up to 30 titles and created not earlier than 24 hours before launching the automated search. I.e., roughly speaking, we need to know what people have written on the Internet for the last day. The input data will be the following:
  • The list of site addresses - url's of sites involved into the search.
  • The list of key phrases - the phrases in Russian and/or English the search relies on.
  • The list of undesirable words - the words that must be excluded from the search results.
The idea
There are a lot of sources on the Internet that offer their services of search and it is reasonable to use their capabilities to solve our task. We have chosen because it is, in our opinion, the most suitable one.
Search in google
The working principle of Google is the same as of any other search engine: you type a request for Google and it gives you the answer. The search engine has flexible settings to make it easier to form the necessary request.
Search parameters in google
Let us look at the most interesting (within the scope of our task) search parameters: The address itself
as_q The key phrase (it is the phrase - not a line of words)
Num The number of results to be shown on the page
as_eq The words that must be excluded from the search results
as_sitesearch url of the site involved into the search
The search engine has some other parameters but in our case they are irrelevant. Here is an example of a request in Google with the search parameters:

How we can use it
The conclusion from all said above is that we can automate the search process using the Google search. The algorithm is as follows:
  • We form a request to Google relying on the input data.
  • The request is processed.
  • The result is processed (parsing of the html-page).
  • All the previous steps are repeated for every site and every key phrase of the input data.
The script is written in php.
Input data
There are three types of the input data. These are the list of the url's of the sites to perform the search on, the list of key phrases and the list of the words that must be excluded from the search results. To present these data we use an xml file like this:
<?xml version="1.0" encoding="utf-8"?>
<search_params lang="ru">
<phrase>"64-bit" c++</phrase>
<phrase>64-bit migration</phrase>
XML parsing
An XML file has a simple structure and small size, so you may use the script PHP Simple HTML DOM Parser.
It is described in the documentation how to use the script but we should notice that the techniques of using it with DOM are very similar to how JQuery, a popular javascript library, does it. For example, the following code receives all the links from the html page by the address and prints them on the screen:
// get DOM from URL or file
$html = file_get_html('');
// find all link
foreach($html->find('a') as $e)
echo $e->href . '<br>';
But there is one issue related to memory occurring when Simple HTML DOM Parser works. The point is that the function file_get_html creates a new object of simple_html_dom class at every call and if you use this function in a loop, memory runs out. Due to an unknown reason we cannot force it to free. So the most reasonable solution is not to use this function in a loop and call it once and work only with one object of the class simple_html_dom.
Script creation
Actually there is nothing interesting about it - it is a common script in php written using MVC pattern. The source code is also simple.
The user interface is very simplified: when you address a page, you see only one button "Send Request" (in the browser window) and after you click it, the result appears on the screen in a couple of seconds.
Having introduced this script, we can now always see what has happened in the world in our scope (64-bit and parallel programming) for the last twenty-four hours.
Tags: Google, html, Php, script
Andrey2008 31 january 2012, 17:00
Vote for this post
Bring it to the Main Page


Leave a Reply

Avaible tags
  • <b>...</b>highlighting important text on the page in bold
  • <i>..</i>highlighting important text on the page in italic
  • <u>...</u>allocated with tag <u> text shownas underlined
  • <s>...</s>allocated with tag <s> text shown as strikethrough
  • <sup>...</sup>, <sub>...</sub>text in the tag <sup> appears as a superscript, <sub> - subscript
  • <blockquote>...</blockquote>For  highlight citation, use the tag <blockquote>
  • <code lang="lang">...</code>highlighting the program code (supported by bash, cpp, cs, css, xml, html, java, javascript, lisp, lua, php, perl, python, ruby, sql, scala, text)
  • <a href="http://...">...</a>link, specify the desired Internet address in the href attribute
  • <img src="http://..." alt="text" />specify the full path of image in the src attribute