0

Hey I'm trying to do something with searching Google using Python, and I found this code that mostly comes up errorless, but I'm getting the error

(TypeError: 'NoneType' object is not subscriptable)

and dont know what to do. Thanks in advance for any help.

import urllib.request
import json as m_json
query = input ( 'Query: ' )
query = urllib.parse.urlencode ( { 'q' : query } )
response = urllib.request.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 )
James Jones
  • 3,850
  • 5
  • 25
  • 44

1 Answers1

0

After some tries Google will block your queries, so the response is None and your code will not work.

To web scraping Google results you will need a more complex solution using a list of proxies to change your IP, manage cookies, etc.

Here you will find a more accurate solution to this: Is it ok to scrape data from Google results?

Gui
  • 763
  • 1
  • 7
  • 20