import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse
import json
import sys
import webbrowser


api_key, userip = None, None
query = {'q' : 'great climbing in new york'}

# by default, you learn about climbing, but if you give an argument, you learn
# about other stuff.
if 1 < len(sys.argv):
  query['q'] = sys.argv[1]
referrer = "http://cs.nyu.edu/q/3900610"

if userip:
    query.update(userip=userip)
if api_key:
    query.update(key=api_key)

url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % (
    urllib.parse.urlencode(query))

request = urllib.request.Request(url, headers=dict(Referer=referrer))
json = json.load(urllib.request.urlopen(request))

for x in json['responseData']['results']:
  print(x)

print('           ')
print('_______________')
print('           ')

for x in json['responseData']['results']:
  print(x['titleNoFormatting'])
  print(x['url'])

results = json['responseData']['results']
url=results[3]['url']
webbrowser.open_new(url)


