Google's "define: " through an API?
Asked Answered
P

3

55

I want to get the result of searches that use special features in Google, like "define: [phrase]" and I can't seem to find relevant information about this.

Does anyone knows where I can get the data in JSON format (like the rest of Google's APIs) without scraping the results page manually?

Thanks, Eli

Patriotism answered 28/7, 2011 at 13:33 Comment(4)
is this what you're looking for? code.google.com/apis/customsearch/v1/overview.htmlMooring
@Emanuel You might want to check for the updated answer below...Ecotone
So Google Dictionary is no longer a thing so how can the answer to this question be possible now?Berkeleian
I created dictionaryapi.dev, you can take a look at that, it is multilingual dictionary API, which provide data from google. Source code is on github.Asafetida
E
34

I wish I had not set a bounty for this, because I stumbled upon the answer a few days later and it is really simple.

Here is URL to call if you want a definition to love:

http://www.google.com/dictionary/json?callback=a&sl=en&tl=en&q=love

You will get a response stream containing JSONP, with the following contents ("prettified" for learning purposes, API returns all in one line):

a(
    {
       "query":"love",
       "sourceLanguage":"en",
       "targetLanguage":"en",
       "primaries":[
          {
             "type":"headword",
             "terms":[
                {
                   "type":"text",
                   "text":"love",
                   "language":"en",
                   "labels":[
                      {
                         "text":"Noun",
                         "title":"Part-of-speech"
                      }
                   ]
                },
                {
                   "type":"phonetic",
                   "text":"/ləv/",
                   "language":"und"
                },
                {
                   "type":"sound",
                   "text":"http://www.gstatic.com/dictionary/static/sounds/de/0/love.mp3",
                   "language":"und"
                }
             ],
             "entries":[
                {
                   "type":"related",
                   "terms":[
                      {
                         "type":"text",
                         "text":"loves",
                         "language":"und",
                         "labels":[
                            {
                               "text":"plural"
                            }
                         ]
                      }
                   ]
                },
                {
                   "type":"meaning",
                   "terms":[
                      {
                         "type":"text",
                         "text":"An intense feeling of deep affection",
                         "language":"en"
                      }
                   ],
                   "entries":[
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"babies fill parents with intense feelings of \x3cem\x3elove\x3c/em\x3e",
                               "language":"en"
                            }
                         ]
                      },
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"their \x3cb\x3e\x3cem\x3elove\x3c/em\x3e for\x3c/b\x3e their country",
                               "language":"en"
                            }
                         ]
                      }
                   ]
                },
                {
                   "type":"meaning",
                   "terms":[
                      {
                         "type":"text",
                         "text":"A deep romantic or sexual attachment to someone",
                         "language":"en"
                      }
                   ],
                   "entries":[
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"it was \x3cem\x3elove\x3c/em\x3e at first sight",
                               "language":"en"
                            }
                         ]
                      },
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"they were both \x3cb\x3ein \x3cem\x3elove\x3c/em\x3e with\x3c/b\x3e her",
                               "language":"en"
                            }
                         ]
                      },
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"we were slowly \x3cb\x3efalling in \x3cem\x3elove\x3c/em\x3e\x3c/b\x3e",
                               "language":"en"
                            }
                         ]
                      }
                   ]
                },
                {
                   "type":"meaning",
                   "terms":[
                      {
                         "type":"text",
                         "text":"A personified figure of \x3cem\x3elove\x3c/em\x3e, often represented as Cupid",
                         "language":"en"
                      }
                   ]
                },
                {
                   "type":"meaning",
                   "terms":[
                      {
                         "type":"text",
                         "text":"A great interest and pleasure in something",
                         "language":"en"
                      }
                   ],
                   "entries":[
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"his \x3cb\x3e\x3cem\x3elove\x3c/em\x3e for\x3c/b\x3e football",
                               "language":"en"
                            }
                         ]
                      },
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"we share a \x3cb\x3e\x3cem\x3elove\x3c/em\x3e of\x3c/b\x3e music",
                               "language":"en"
                            }
                         ]
                      }
                   ]
                },
                {
                   "type":"meaning",
                   "terms":[
                      {
                         "type":"text",
                         "text":"Affectionate greetings conveyed to someone on one\x27s behalf",
                         "language":"en"
                      }
                   ]
                },
                {
                   "type":"meaning",
                   "terms":[
                      {
                         "type":"text",
                         "text":"A formula for ending an affectionate letter",
                         "language":"en"
                      }
                   ],
                   "entries":[
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"take care, lots of \x3cem\x3elove\x3c/em\x3e, Judy",
                               "language":"en"
                            }
                         ]
                      }
                   ]
                },
                {
                   "type":"meaning",
                   "terms":[
                      {
                         "type":"text",
                         "text":"A person or thing that one \x3cem\x3eloves\x3c/em\x3e",
                         "language":"en"
                      }
                   ],
                   "entries":[
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"she was \x3cb\x3ethe \x3cem\x3elove\x3c/em\x3e of his life\x3c/b\x3e",
                               "language":"en"
                            }
                         ]
                      },
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"their two great \x3cem\x3eloves\x3c/em\x3e are tobacco and whiskey",
                               "language":"en"
                            }
                         ]
                      }
                   ]
                },
                {
                   "type":"meaning",
                   "terms":[
                      {
                         "type":"text",
                         "text":"A friendly form of address",
                         "language":"en"
                      }
                   ],
                   "entries":[
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"it\x27s all right, \x3cem\x3elove\x3c/em\x3e",
                               "language":"en"
                            }
                         ]
                      }
                   ]
                },
                {
                   "type":"meaning",
                   "terms":[
                      {
                         "type":"text",
                         "text":"Used to express affectionate approval for someone",
                         "language":"en"
                      }
                   ],
                   "entries":[
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"don\x27t fret, there\x27s a \x3cem\x3elove\x3c/em\x3e",
                               "language":"en"
                            }
                         ]
                      }
                   ]
                },
                {
                   "type":"meaning",
                   "terms":[
                      {
                         "type":"text",
                         "text":"(in tennis, squash, and some other sports) A score of zero; nil",
                         "language":"en"
                      }
                   ],
                   "entries":[
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"\x3cem\x3elove\x3c/em\x3e fifteen",
                               "language":"en"
                            }
                         ]
                      },
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"he was down two sets to \x3cem\x3elove\x3c/em\x3e",
                               "language":"en"
                            }
                         ]
                      }
                   ]
                }
             ]
          },
          {
             "type":"headword",
             "terms":[
                {
                   "type":"text",
                   "text":"love",
                   "language":"en",
                   "labels":[
                      {
                         "text":"Verb",
                         "title":"Part-of-speech"
                      }
                   ]
                },
                {
                   "type":"phonetic",
                   "text":"/ləv/",
                   "language":"und"
                },
                {
                   "type":"sound",
                   "text":"http://www.gstatic.com/dictionary/static/sounds/de/0/love.mp3",
                   "language":"und"
                }
             ],
             "entries":[
                {
                   "type":"related",
                   "terms":[
                      {
                         "type":"text",
                         "text":"loved",
                         "language":"und",
                         "labels":[
                            {
                               "text":"past participle"
                            }
                         ]
                      },
                      {
                         "type":"text",
                         "text":"loves",
                         "language":"und",
                         "labels":[
                            {
                               "text":"3rd person singular present"
                            }
                         ]
                      },
                      {
                         "type":"text",
                         "text":"loving",
                         "language":"und",
                         "labels":[
                            {
                               "text":"present participle"
                            }
                         ]
                      },
                      {
                         "type":"text",
                         "text":"loved",
                         "language":"und",
                         "labels":[
                            {
                               "text":"past tense"
                            }
                         ]
                      }
                   ]
                },
                {
                   "type":"meaning",
                   "terms":[
                      {
                         "type":"text",
                         "text":"Feel a deep romantic or sexual attachment to (someone)",
                         "language":"en"
                      }
                   ],
                   "entries":[
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"do you \x3cem\x3elove\x3c/em\x3e me?",
                               "language":"en"
                            }
                         ]
                      }
                   ]
                },
                {
                   "type":"meaning",
                   "terms":[
                      {
                         "type":"text",
                         "text":"Like very much; find pleasure in",
                         "language":"en"
                      }
                   ],
                   "entries":[
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"I\x27d \x3cem\x3elove\x3c/em\x3e a cup of tea, thanks",
                               "language":"en"
                            }
                         ]
                      },
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"I just \x3cem\x3elove\x3c/em\x3e dancing",
                               "language":"en"
                            }
                         ]
                      },
                      {
                         "type":"example",
                         "terms":[
                            {
                               "type":"text",
                               "text":"a fun-\x3cem\x3eloving\x3c/em\x3e girl",
                               "language":"en"
                            }
                         ]
                      }
                   ]
                }
             ]
          }
       ]
    }
,200,null)

Google Dictionary API returns in the form of a callback function (JSONP). Before you can process the JSON within the function call, you have to strip out the callback function data.

That means removing everything before the first { and everything after the last }.

After this you are done! That's all information you need and it's basically the same (actually a little more!) information when you search Google for define love.

Ecotone answered 3/5, 2012 at 6:13 Comment(11)
From the JSON response above, what does \x3cb\x3e\x3cem\x3elove\x3c/em\x3e means?Liger
Same Question, Please Define \x3cb\x3e\x3cem\x3elove\x3c/em\x3e? this is unwanted string and I want to remove all this type of codes/string. Is there any list avilable of this type of codes so we can erase all of them from our output. ThanksFreeloader
Thanks for this! I was considering paying the $100 a month that the Oxford English Dictionary charge for their API! And WordNik's API is creaky.Brinson
Though not quite relevant, Is there any site using this json call.Inveigh
were you able to parse this json?Way
Looks like they got rid of it today.Bermejo
The response was relevant to it's date, but it appear to no longer work.Patriotism
Merriam-Webster's Collegiate Dictionary gives an api with 1000 free queries each day: dictionaryapi.com/products/api-collegiate-dictionary.htmAcadia
@aardvarkk See my answer below.Asafetida
@Patriotism I have created dictionary api from google, I have posted it as an answer below.Asafetida
@ZackZatkin-Gold I also needed Google Dictionary API for my project, it was not present so I decided to create one. See the answer below.Asafetida
L
8
from bs4 import BeautifulSoup
import  requests
word = "democracy"
url = 'https://www.google.co.in/search?q=define%20' + word + '#cns=1'
response = requests.get(url, headers={"user-agent":"Mozilla/5.0(Macintosh; Intel Mac OS X 10.12; rv:49.0) Gecko/20100101 Firefox/49.0"})
html = response.content
final_soup = BeautifulSoup(html,"html5lib")
everyThing = final_soup.select("div._Jig")
for line in everyThing:
    print("-",line.text)

I wrote this script to get the definition of a word from google. Hope it helps but its not using any API.

Lumbricalis answered 25/11, 2016 at 3:59 Comment(0)
T
0

For anybody coming back to this, all previous answers seem to be broken. Here's what I have working as of april 2022:

import bs4
import requests


def definition(word):
    URL = "https://www.google.com/search?q=define+" + word
    page = requests.get(URL)
    soup = bs4.BeautifulSoup(page.text, 'html.parser')
    first_find = soup.find('ol', class_='yRG22b v7pIac')
    if (first_find is not None):
        return first_find.find('div', class_='BNeawe s3v9rd AP7Wnd').getText()
    return None

definition(word) returns a string which is the first definition result if you were to type "define (word)" into google. If google has no definition for that word, it returns None. Apologies if I violate any python standard practices, I'm a beginner to this language.

I'm assuming this will break in a month when google slightly modifies their html.

Talton answered 16/4, 2022 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.