Google's "define: " through an API?

Viewed 12324

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

3 Answers

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.

Related