谷歌通过API“定义:”?

时间:2011-07-28 13:33:19

标签: json google-api google-search-api

我想获得使用Google特殊功能的搜索结果,例如“define:[phrase]”,我似乎无法找到相关信息。

有没有人知道我在哪里可以获得JSON格式的数据(比如Google的其他API),而无需手动抓取结果页面?

谢谢, 利

2 个答案:

答案 0 :(得分:36)

我希望我没有为此付出赏金,因为几天后我偶然发现了答案,这很简单。

如果您想要的定义:

,这是要调用的网址
  

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

您将获得一个包含JSONP的响应流,其中包含以下内容(为了学习目的,“美化”,API在一行中返回):

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以回调函数(JSONP)的形式返回。在函数调用中处理JSON之前,必须去掉回调函数数据。

这意味着删除第一个{之前的所有内容以及最后一个}之后的所有内容。

在此之后你就完成了!这是您需要的所有信息,当您在Google上搜索define love时,这些信息基本相同(实际上多一点!)信息。

答案 1 :(得分:5)

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)

我写了这个脚本来从谷歌获取一个单词的定义。 希望它有所帮助,但它不使用任何API。