11

Is there a way to fetch the first image source from google search if I have a search term?

For example if input is tomato, ouput would be http://www.cksinfo.com/clipart/food/fruits/tomatoes/tomato.png

Thanks!

4 Answers 4

6

Google Custom Search API now includes images. You are limited to 100 queries/day before billing, but it is the only legitimate way to use Google to do your searching for you.

If the link misdirects:

Calling styles

There is more than one way to invoke the API:

REST

Representational State Transfer, in the Google Custom Search API is somewhat different from traditional REST. Instead of providing access to resources, the API provides access to a service. As a result, the API provides a single URI that acts as the service endpoint.

You access the Google Custom Search API service endpoint using the GET REST HTTP verb, as described in API operations. You pass in the details of all search requests as query parameters.

The specific format for the single Google Custom Search API URI is:

https://www.googleapis.com/customsearch/v1?parameters

where parameters are any parameters to apply to the query. See Working with search results and and Query parameter reference in the Using REST document for details.

Here is an example of how this works in the Google Custom Search API, which searches a test Custom Search Engine for lectures:

GET https://www.googleapis.com/customsearch/v1?key=INSERT-YOUR-KEY&cx=017576662512468239146:omuauf_lfve&q=lectures

REST from JavaScript

You can invoke the Google Custom Search API using REST from JavaScript, using the callback query parameter and a callback function. This allows you to write rich applications that display Custom Search data without writing any server side code.

The following example uses this approach to display the first page of search results for the query tomato:

<html>
  <head>
    <title>JSON/Atom Custom Search API Example</title>
  </head>
  <body>
    <div id="content"></div>
    <script>
      function hndlr(response) {
      for (var i = 0; i < response.items.length; i++) {
        var item = response.items[i];
        // in production code, item.htmlTitle should have the HTML entities escaped.
        document.getElementById("content").innerHTML += "<br>" + item.htmlTitle;
      }
    }
    </script>
    <script src="https://www.googleapis.com/customsearch/v1?key=YOUR-KEY&cx=017576662512468239146:omuauf_lfve&q=tomato&callback=hndlr">
    </script>
  </body>
</html>

You would, of course, update the src of the script to include the value of the input from which you are searching.

1
  • should we dispose the API key publicly in the script url ? o_O
    – T.Todua
    Sep 10, 2018 at 10:12
3

You can use the Google Image Search API to do this. Unfortunately it has been depracated so you may find the number of queries per day you can make to be limited. I am unsure if there is a new API to replace it.

3
  • There's been no replacement, and this particular API is actually on the list of ones that will definitely be shut down(versus just deprecated with no further schedule for shutdown), per the recent API "spring cleaning" post, though that's likely to be in the range of three years out(see section 1.3).
    – Su'
    Aug 24, 2011 at 17:20
  • Hm ok, but can I maybe somehow GET the content of results page and parse the first image link or something? Any workaround at all?
    – 3mpetri
    Aug 25, 2011 at 11:55
  • You could do a search and then parse the HTML to get the first result. That is definitely doable. It probably violates Google's TOS, though.
    – John Conde
    Aug 25, 2011 at 11:59
1

You can use Serpdog's Google Images API to scrape the sources of Google Images. Serpdog. It also gives you a free 100 request credits on the first sign-up.

Example of cURL request:

cURL "https://api.serpdog.io/images?api_key=APIKEY&q=football&gl=us"

Results:

"image": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS_Tu78LWxIu_M_sN_kMfj2guqIbu2VcSLyI84CQGbuFRIyTCVR&s",
"title": "Football - Wikipedia",
"source": "en.wikipedia.org",
"link": "https://en.wikipedia.org/wiki/Football",
"original": "https://upload.wikimedia.org/wikipedia/commons/b/b9/Football_iu_1996.jpg",
"rank": 1
  },
  {
    "image": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTxvsz_pjLnFyCnYyCxxY5rSHQCHjNJyYGFZqhQUtTm0XOzOWw&s",
    "title": "Soft toy, American football/brown - IKEA",
    "source": "www.ikea.com · In stock",
    "link": "https://www.ikea.com/us/en/p/oenskad-soft-toy-american-football-brown-90506769/",
    "original": "https://www.ikea.com/us/en/images/products/oenskad-soft-toy-american-football-brown__0982285_pe815602_s5.jpg",
   "rank": 2
  },
  ......

Disclosure: I am the founder of serpdog.io

0
0

You can extract the image sources from Google Images via google-search-results package by SerpApi. It's a paid service with a free trial.

from serpapi import GoogleSearch
import os

params = {
    "engine": "google",
    "q": "coffee",
    "tbm": "isch",
    "api_key": os.getenv("API_KEY")
}

client = GoogleSearch(params)
data = client.get_dict()

print("Images results")

for result in data['images_results']:
    print(f"""
Position: {result['position']}
Original image: {result['original']}
""")

Output

Images results

Position: 1
Original image: https://upload.wikimedia.org/wikipedia/commons/4/45/A_small_cup_of_coffee.JPG


Position: 2
Original image: https://media3.s-nbcnews.com/j/newscms/2019_33/2203981/171026-better-coffee-boost-se-329p_67dfb6820f7d3898b5486975903c2e51.fit-1240w.jpg

...

Disclosure: I work at SerpApi.

Not the answer you're looking for? Browse other questions tagged or ask your own question.