How can I use Google's image API to custom search for images and import them all?

  • $\begingroup$ I used to just Import[] a search result page, but this is not possible anymore... Seems like v9 should have integration with google.images api $\endgroup$ – M.R. Mar 1 '13 at 21:33
  • 2
    $\begingroup$ This is a good question, why have people downvoted this? Scraping images is very common tool that people need. $\endgroup$ – M.R. Mar 1 '13 at 21:34
  • $\begingroup$ I don't know, this is an operation that most image processing people need. wolfram|alpha query and exampledata will not suffice most times... $\endgroup$ – user4364 Mar 1 '13 at 21:42
  • 6
    $\begingroup$ I think people are downvoting because you didn't try anything or post what you have done before submitting the question. $\endgroup$ – s0rce Mar 1 '13 at 22:06

Mathematica now supports a native connection to GoogleCustomSearch API.

To use image search you can do:

gs = ServiceConnect["GoogleCustomSearch"]
gs["Search",{"Query"-> "Jennifer Lawrence","SearchType"-> "Image"}]

enter image description here

To use GoogleCustomSearch API you need an API Key and a Custom search engine ID.

To get the API Key you first have to go to https://console.developers.google.com and create a project if you don't have one yet. Once you have the project (asuming you are using the new console interface), click the Credentials menu at the left. There you'll see all your credentials. If don't have one, click the Add credentials and, for GoogleCustomSearch, you need the API key Browser key type.

For the Custom search engine ID, you have to go to https://cse.google.com/all and add a search engine. Once you create it, just click on that and you'll get a screen with a lot of options. There's a button there that says Search engine ID. Click it and you'll get the ID you need.

Here is the official Wolfram documentation for Google API.

You may also want to try the BingSearch service also available from Mathematica. The functionality is basically the same. Here is the official documentation for that.

  • 2
    $\begingroup$ Can you give me some help on the "add a search engine" part? Specifically I'm stuck on the "sites to search" option, whereas I just want to search the web to get the results lik eyou have there $\endgroup$ – Jason B. Nov 19 '15 at 12:01
  • $\begingroup$ Yeah, it's a bit confusing. You can add there any website you want. As a test, just add www.wikipedia.org for example. Give a name to your search engine and create it. After that go to "Modify your search engine" (Control Panel button) and once there, in the Sites to search section you'll see a select box with the option "Search only included sites". Change that option to "Search the entire web but emphasize included sites". With that you'll be able to search the entire web. Try that and let me know if you get better results. $\endgroup$ – xtian777x Nov 19 '15 at 22:29
  • $\begingroup$ Tip: For the Custom Search Engine ID. After choosing https://www.google.com/ and opening the control panel You need to toggle the Image Search toggle button. $\endgroup$ – Conor Cosnett Dec 19 '20 at 13:09

Using the (deprecated but easy) API documented here,

result = Import[
  "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=fuzzy%20monkey", "JSON"]

Import["url" /. ("results" /. ("responseData" /. result))[[1]]]

Mathematica graphics

If you hit the service too many times, it'll lock you out for a while.

Here's another example that gives the 24th-32nd results:

result = Import["https://ajax.googleapis.com/ajax/services/search/images?v=1.0&start=24&rsz=8&q=fuzzy+monkey", "JSON"];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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