1

I am using google search api But by default it shows 4 and maximum 8 results per page. I want more results per page.

Joey
  • 344,408
  • 85
  • 689
  • 683
user12345
  • 2,400
  • 8
  • 33
  • 40

1 Answers1

0

Add the rsz=8 parameter to this google search demonstration code, then use the start=... parameter to control which group of results you receive.

This, for example, gives you 50 results:

import urllib
import json
import sys
import itertools

def hits(astr):
    for start in itertools.count():
        query = urllib.urlencode({'q':astr, 'rsz': 8, 'start': start*8})
        url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s'%(query)
        search_results = urllib.urlopen(url)
        results = json.loads(search_results.read())
        data = results['responseData']
        if data:
            hits = data['results']
            for h in hits:
                yield h['url']
        else:
            raise StopIteration


def showmore(astr,num):
    for i,h in enumerate(itertools.islice(hits(astr),num)):
        print('{i}: {h}'.format(i=i,h=h))

if __name__=='__main__':
    showmore(sys.argv[1],50)
Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • The question is, as far as I understand, how to get *more* than 8 results in one query. – dpq Sep 02 '10 at 10:59
  • Oops, I misunderstood the question... This post will self-destruct in 30 seconds... – unutbu Sep 02 '10 at 11:00
  • list index out of range i am getting – user12345 Sep 02 '10 at 12:10
  • @alis: If you save the above script as `test.py`, then run it like this: `test.py searchterm`. If you just run `test.py`, then yes, you get a `IndexError: list index out of range` exception. – unutbu Sep 02 '10 at 12:34