"Improve your... Google?"
- The idea
- Search in google
- Search parameters in google
- How we can use it
- Input data
- XML parsing
- Script creation
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.
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 google.com 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
The key phrase (it is the phrase - not a line of words)
The number of results to be shown on the page
The words that must be excluded from the search results
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:
http://www.google.com/search?as_q=64-bit+portability+& hl=ru&newwindow=1& num=30&btnG=%D0%9F%D0%BE%D0% B8%D1%81%D0%BA+%D0%B2+Google& as_epq=&as_oq=& as_eq=%D0%BA%D1%83%D0%BF%D0%B8%D1%82% D1%8C+%D1%81%D0%BA%D0%B0%D1%87%D0% B0%D1%82%D1%8C+& lr=lang_ru&cr=&as_ft=i&as_filetype=& as_qdr=d&as_occt=any& as_dt=i&as_sitesearch=http://www.codeguru.com/& as_rights=&safe=images
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.
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"> <sites> <url>http://www.dreamincode.net</url> <url>http://forum.vingrad.ru/</url> <url>http://forum.sources.ru/</url> <url>http://groups.google.com/</url> </sites> <words> <white_list> <phrase>"64-bit" c++</phrase> <phrase>64-bit migration</phrase> <phrase>viva64</phrase> </white_list> <black_list> <phrase>buy</phrase> <phrase>download</phrase> </black_list> </words> </search_params>
An XML file has a simple structure and small size, so you may use the script PHP Simple HTML DOM Parser.
include('../simple_html_dom.php'); // get DOM from URL or file $html = file_get_html('http://www.google.com/'); // 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.
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.