-1

I am trying to run below script:

import urllib
import json as m_json
query = raw_input ( 'Query: ' )
query = urllib.urlencode ( { 'q' : query } )
response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]
for result in results:
    title = result['title']
    url = result['url']   # was URL in the original and that threw a name error exception
    print ( title + '; ' + url )

My output is:

Query: test
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    results = json [ 'responseData' ] [ 'results' ]
TypeError: 'NoneType' object has no attribute '__getitem__'
user000001
  • 32,226
  • 12
  • 81
  • 108
Alfy
  • 33
  • 1
  • 6
  • The Google Web Search API is no longer available. Please migrate to the Google Custom Search API – taras Apr 29 '17 at 10:22
  • `json['responseData']` is `None`. See [here](http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=test). – Maroun Apr 29 '17 at 10:40

2 Answers2

2

After printing out the raw json with the following code:

print(str(json))

we get the following response from google:

{'responseData': None, 'responseDetails': 'The Google Web Search API is no longer available. Please migrate to the Google Custom Search API (https://developers.google.com/custom-search/)', 'responseStatus': 403}

A solution can be found here:

What are the alternatives now that the Google web search API has been deprecated?

Community
  • 1
  • 1
0

You can print the result and you will see the following:

The Google Web Search API is no longer available. Please migrate to the Google Custom Search API (https://developers.google.com/custom-search/)

Try that ;) good luck

PS: For more info, you may like to visit this detailed tutorial in another stack overflow question

Community
  • 1
  • 1
Filip Happy
  • 575
  • 5
  • 17