Goolgle knowledge graph API使用
生活随笔
收集整理的這篇文章主要介紹了
Goolgle knowledge graph API使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
試了一下google的知識圖譜API的使用,簡單的整理了一下:
第一步:安裝google的google-api-python-client包
pip install –upgrade google-api-python-client
第二步:創(chuàng)建API
在google的個人應用中心創(chuàng)建一個應用
https://console.developers.google.com/projectselector/apis/credentials?supportedpurview=project
在庫中搜索Knowledge Graph Search API,并將該API授權(quán)給你創(chuàng)建的應用。
拷貝你應用的API key
第三步:調(diào)用google的API完成你需要的程序需求
將剛才的申請到的API key添加到你的應用中
import json import urllibclass GooleKGAPI(object):def __init__(self):self.api_key = open('.api_key').read()def getResult(self,query,type):service_url = 'https://kgsearch.googleapis.com/v1/entities:search'params = {'query': query,'limit': 10,'indent': True,'key': self.api_key,}#https://kgsearch.googleapis.com/v1/entities:search?query=taylor+swift&key=API_KEY&limit=1&indent=Trueurl = service_url + '?' + urllib.urlencode(params)#print urlresponse = json.loads(urllib.urlopen(url).read())print responsefor element in response['itemListElement']:print element['result']['name'] + ' (' + str(element['resultScore']) + ')'##test if __name__ == "__main__":gkg = GooleKGAPI()# example => bill gatesgkg.getResult("Taylor Swift","Person")總結(jié)
以上是生活随笔為你收集整理的Goolgle knowledge graph API使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android Instant Run介
- 下一篇: #EDA(电子设计自动化)