Pastebin

Guest User

a guest

Nov 21st, 2009

1,306

0

Never

Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

  1. from time import sleep

  2. from pprint import pprint

  3. import json

  4. import urllib

  5. import json

  6. def showsome(searchfor):

  7. query = urllib.urlencode({'q': searchfor})

  8. url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query

  9. search_response = urllib.urlopen(url)

  10. search_results = search_response.read()

  11. results = json.loads(search_results)

  12. data = results['responseData']

  13. pprint(data)

  14. #print 'Total results: %s' % data['cursor']['estimatedResultCount']

  15. hits = data['results']

  16. #print 'Top %d hits:' % len(hits)

  17. #for h in hits: print ' ', h['url']

  18. #print 'For more results, see %s' % data['cursor']['moreResultsUrl']

  19. return data['cursor']['estimatedResultCount']

  20. sizes = {}

  21. for i in range(4):

  22. try:

  23. query = 'a' * i

  24. sizes[query] = int(showsome(query))

  25. sleep(1)

  26. except Exception:

  27. sizes[query] = 'exception'

  28. def jsonize_xy(items):

  29. points_list = []

  30. for point in items:

  31. x, y = point

  32. d = {

  33. "x": x,

  34. "y": y,

  35. }

  36. points_list.append(d)

  37. return json.dumps(points_list)

  38. points = [ ((len(key), val)) for key,val in sizes.items() if val != 'exception' ]

  39. points.sort()

  40. open('output.json', 'w').write(jsonize_xy(points))

Read the original on pastebin.com ↗