Advertisement
jroakes

kgsearch_example

May 17th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. ## Example of Python client calling Knowledge Graph Search API.###############################
  2.  
  3. import json
  4. import urllib
  5. import urllib.request
  6.  
  7. api_key = '**********************************************'
  8. service_url = 'https://kgsearch.googleapis.com/v1/entities:search'
  9.  
  10. '''
  11. The Knowledge Graph API Search API allows developers a free quota of up to 100,000 (one hundred thousand) read calls per day per project. We believe this meets the needs of the vast majority of developers.
  12. '''
  13.  
  14. # Name to query (str)
  15. query = 'jeremy'
  16.  
  17. params = {
  18.     'query': query,
  19.     'limit': 20,
  20.     'indent': True,
  21.     'key': api_key,
  22. }
  23. url =  "{}{}{}". format(service_url, '?', urllib.parse.urlencode(params))
  24. data = json.loads(urllib.request.urlopen(url).read())
  25.  
  26. result = [
  27.             {'name':item['result'].get('name',''),
  28.              'description':item['result']['detailedDescription'].get('articleBody', ''),
  29.              'image':item['result'].get('image', '').get('contentUrl') if 'contentUrl' in item['result'].get('image', '') else item['result'].get('image', '') }
  30.              for item in data['itemListElement']
  31.              if "Person" in item['result']['@type']
  32.          ]
  33.  
  34. print(json.dumps(result[:3], indent=2, sort_keys=True))
  35.  
  36.  
  37. ## Example Output ################################################################
  38. '''
  39. [
  40.  {
  41.    "description": "Jeremy Bernard Corbyn is a British politician serving as Leader of the Labour Party and Leader of the Opposition since 2015. Corbyn was first elected Member of Parliament for Islington North in 1983. ",
  42.    "image": "http://t0.gstatic.com/images?q=tbn:ANd9GcQyAH71ISMcdvSUkM_ndk8s38nd4tspRNJZ3Vi9KUx72Pmbqo5g",
  43.    "name": "Jeremy Corbyn"
  44.  },
  45.  {
  46.    "description": "Jeremy Charles Robert Clarkson is an English broadcaster, journalist and writer who specialises in motoring. He is best known for co-presenting the BBC TV show Top Gear with Richard Hammond and James May from October 2002 to March 2015. ",
  47.    "image": "http://t3.gstatic.com/images?q=tbn:ANd9GcTkIpzT0beHgqJWVkTTjg71OFDMV-w_px6PIjzGZndZGGkIOpCQ",
  48.    "name": "Jeremy Clarkson"
  49.  },
  50.  {
  51.    "description": "Jeremy Richard Streynsham Hunt is a British Conservative Party politician serving as British Foreign Secretary since 2018 and Member of Parliament for South West Surrey since 2005. ",
  52.    "image": "http://t0.gstatic.com/images?q=tbn:ANd9GcRIP0Amx6SZDtyUsw4Q7idJGj4yJ_NTiVz05b8HOBD3HNWRUymN",
  53.    "name": "Jeremy Hunt"
  54.  }
  55. ]
  56. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement