"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.
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 ideaThere 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 googleThe 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 googleLet us look at the most interesting (within the scope of our task) search parameters:
|http://www.google.com/search?||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|
How we can use itThe 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.
ImplementationThe script is written in php.
Input dataThere 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"?>
XML parsingAn 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');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.
// 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>';
Script creationActually 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.
ConclusionHaving 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.
|Vote for this post
Bring it to the Main Page