Looking for an API for Google Images, I was surprised to find there isn't one. I discovered a project in ASP.NET from a few years ago which shows how difficult life can be if your language doesn't play well with XML.
A few lines of XQuery gets a sub-set of the thumbnails for a given search :
declare function local:google-images( $search as xs:string, $start as xs:integer, $n as xs:integer) as element(a)* { let $response := httpclient:get( xs:anyURI( concat("http://images.google.com/images?q=", encode-for-uri($search), "&start=",$start) ) ,false(), () ) return if ($response/@statusCode eq "200") then ($response/httpclient:body//h2[@class="hd"]/following-sibling::div[1]//a) [position() <= $n] else () } );
This function hides the construction of the search url and the parsing of the HTML page. The eXist function httpclient:get tidies the HTML so it is well-formed. Inspection of the HTML allows construction of a suitable XPath expression to scrape the thumbnail code out of the page.
The function can then be used in a script, like this one to get a random image from the first 50 results
declare option exist:serialize "method=xhtml media-type=text/html"; ... (: get a random thumbnail :) let $q:= request:get-parameter ("q","") let $start := util:random(50) let $thumbnail := local:google-images($q,$start,1) return $thumbnail/img