How to search images in Google with Javascript?

Asked

Viewed 3,305 times

5

I am developing a web page where it was of interest of the project to display some images of Google, where these would be displayed from some significant name.

I was reading something related and I saw that the Google Image Search API was discontinued. There is something similar or it is still possible to use this API for my purpose?

2 answers

8


This code is crap, however, it works. I did it quickly because I saw that no one answered your question.. About the Google API, you will make some requests and have some errors, this API is paid for, the API you sent up is obsolete, now it is the Custom Search that is in use. So if after a few attempts you fail it may be due to this charge.

Code:

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>

<script>
    //Usando jQuery
    a = $.getJSON( "http://ajax.googleapis.com/ajax/services/search/images?v=2.0&hl=pt-br&cr=countryBR&q=carros")
  .done(function( json ) {
    b = jQuery.parseJSON(a.responseText)
    for ( var i = 0; i < b.responseData.results.length; i++ ) {

        document.write("<img src='"+b.responseData.results[i].unescapedUrl+"' />");
}
  })
  .fail(function( jqxhr, textStatus, error ) {
    document.write("Erro ao capturar JSON");
});

</script>
    </body> 

</html> 

Exemplo

  • It is normal for your browser to block getJSON attempts due to CORS. There are ways to cheat, so I recommend that you use the production server to carry out your tests. http://superuser.com/questions/384871/overriding-access-control-allow-origin-restriction-in-google-chrome

  • This technique above is actually a web scraping. For study purposes, there is no problem with you practicing it, however, if you are making a product (website, web app, etc.) that implements this technique in any part of your application, it will be considered illegal and maybe you (or your customer) They’re being sued for it, so you might want to avoid that headache. Here in the United States, some sites do not consider this practice illegal (need to read the terms), in the absence of this information, it will be considered illegal.

  • Friend @Jancassio, that’s actually not illegal. The access is through a JSON made available by Google, in which Google allows public access, but for large amounts of access offers the service with charge. What is made available via API is not illegal, there is a huge difference. The browser blocks CORS due to security issues, that’s all.

  • I think I traveled in that comment, its implementation is ok. The problem with CORS that you refer to is in the service’s answer or in the request for a Resource (image) provided by the service?

  • It blocks receiving external JSON

2

My suggestion is to look for a specific service for image search and the best I know is Flickr. In addition to the excellent API, there is a huge image base available.

This service for example, is the default image search available on the site itself.

I hope I’ve helped.

  • 2

    This does not answer the question. It would be better to leave this information as a comment rather than an answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.