xgoogle python library upgrade for google image search


Download Article as PDF


Couple of days ago I realized that I need some library that will allow me search google and especially face images. I was previously working a bit with the Google API, however Google offers only 100 requests per day and there are some other limitations. It is very good API if you pay and want to commercialize the thing you have done and don’t mind some restrictions. However, if you try to search Google, download HTML and parse it trough python, it will return that the request is forbidden. So I looked for the solution and found out a very good python library, made couple of years ago by Peteris Krumins called xgoogle. It supports normal Google search and some other things, like blog search etc. However, it did not supported image search. So I take a deep dive into the code, whick Peteris made available on GitHub and decided to extend his lib with couple of more classes.

Code of extended xgoogle library can be found on my forked github repository: https://github.com/nikolamilosevic86/xgoogle

Google image search

First class that I extended is class for image search. Basically library works as normal image search, searching Google, and using browser simulation it workarounds Google’s restriction. Then it parses the page and extracts certain information. In case of image search you can get thumbnail image url and url of page on which the image was found. Here is a simple example how to use the library.

from xgoogle.search import GoogleImageSearch, SearchError
try:
    gs = GoogleImageSearch("quick and dirty")
    gs.results_per_page = 50
    results = gs.get_results()
    for res in results:
        print res.trumb.encode('utf8')
        print res.url.encode('utf8')
        print
except SearchError, e:
    print "Search failed: %s" % e

Google face image search

Similarly to image search works face image search. However, in this case xgoogle library searches only pictures with faces. This is quite useful if you want to find image of some celebrity, actor, musician or history figure. It works completely the same way as image search, just uses one different parameter in the url. Here is example of usage:

from xgoogle.search import GoogleFaceImageSearch, SearchError
try:
    gs = GoogleFaceImageSearch("Eddard Stark")
    gs.results_per_page = 50
    results = gs.get_results()
    for res in results:
        print res.trumb.encode('utf8')
        print res.url.encode('utf8')
        print
except SearchError, e:
    print "Search failed: %s" % e

 

Google video search

Last extension was on video search. This search works similarly as previous ones, however, it uses different parameter in Google search url and it retrieves more information. This type of search will retrieve video name, URL, duration, description of the video, author who published it and date of publication. Simple example of using it is here:

from xgoogle.search import GoogleVideoSearch, SearchError
try:
    gs = GoogleVideoSearch("Iron Maiden")
    gs.results_per_page = 50
    results = gs.get_results()
    for res in results:
        print 'Name: ' + res.name.encode('utf8')
        print 'URL: ' + res.url.encode('utf8')
        print 'Date: ' + res.date.encode('utf8')
        print 'Duration: ' + res.duration.encode('utf8')
        print 'Author: ' + res.author.encode('utf8')
        print 'Description: ' + res.description.encode('utf8')        
        print
except SearchError, e:
    print "Search failed: %s" % e

 

Google face image search API

Since I need API for Google face search, I made one myself using this library a bit more of python code and openshift platform. API can be accessed in the following way:

 

http://cloudapps-inspiratron.rhcloud.com/googlefacesearch_json/Eddard%20Stark

Of course instead of Eddard Stark, you may put any other name. Also I would like to show you how to make application using it. Here is one example of the super simple application: https://inspiratron.org/API/GoogleFaceSearch.html

So how this application works, as you may see it has one input box and it uses ajax to get it’s images. However, ajax from javascript cannot call across domains. So it needs to have some for example php script on same domain, which will call API. This script is fairly simple and looks like this:

<?php
$q = $_GET['q'];
$url = 'http://cloudapps-inspiratron.rhcloud.com/googlefacesearch_json/';
$whole_url = $url.urlencode(htmlentities($q));
$json = file_get_contents($whole_url);
echo $json;

Javascript or jquery in this case will get json from API and it has to parse it and show thumbnails. It does it on the following way:

function googleimage(str){
  var ural = 'FaceSearchAPI.php?q='+encodeURIComponent(str);
 $('#results').empty()
$.get( ural, function(data) {
var obj = jQuery.parseJSON(data);
var searchTerm = obj.searchterm;
var numResults = obj.num_results
var results = obj.results
 $.each(results, function(index, element) {
	    $('#results').append($('<img alt="" />', {
                src: element.trumb
            }));
});});
 }

Div with id results will be populated with images. I hope you enjoyed this little tutorial and that the upgraded xgoogle will be useful to you.

Born in Bratislava, Slovakia, lived in Belgrade, Serbia, now living in Manchester, UK, and visitng the world. Nikola is a great enthusiast of AI, natural language processing, machine learning, web application security, open source, mobile and web technologies. Looking forward to create future. Nikola has done PhD in natural language processing and machine learning at the University of Manchester where he worked for 2 years. In 2020, Nikola moved to Berlin and works in Bayer Pharma R&D as a computational scientist.

Leave a Reply

Your email address will not be published. Required fields are marked *