views:

52

answers:

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.

A: 

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)
unutbu
The question is, as far as I understand, how to get *more* than 8 results in one query.
David Parunakian
Oops, I misunderstood the question... This post will self-destruct in 30 seconds...
unutbu
On the other hand, the problem is not too hard to fix...
unutbu
list index out of range i am getting
alis
@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