a guest
Nov 21st, 2009
1,306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
from time import sleep
from pprint import pprint
import json
import urllib
import json
def showsome(searchfor):
query = urllib.urlencode({'q': searchfor})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
search_response = urllib.urlopen(url)
search_results = search_response.read()
results = json.loads(search_results)
data = results['responseData']
pprint(data)
#print 'Total results: %s' % data['cursor']['estimatedResultCount']
hits = data['results']
#print 'Top %d hits:' % len(hits)
#for h in hits: print ' ', h['url']
#print 'For more results, see %s' % data['cursor']['moreResultsUrl']
return data['cursor']['estimatedResultCount']
sizes = {}
for i in range(4):
try:
query = 'a' * i
sizes[query] = int(showsome(query))
sleep(1)
except Exception:
sizes[query] = 'exception'
def jsonize_xy(items):
points_list = []
for point in items:
x, y = point
d = {
"x": x,
"y": y,
}
points_list.append(d)
return json.dumps(points_list)
points = [ ((len(key), val)) for key,val in sizes.items() if val != 'exception' ]
points.sort()
open('output.json', 'w').write(jsonize_xy(points))