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
2010-09-02 10:54:06
The question is, as far as I understand, how to get *more* than 8 results in one query.
David Parunakian
2010-09-02 10:59:28
Oops, I misunderstood the question... This post will self-destruct in 30 seconds...
unutbu
2010-09-02 11:00:35
On the other hand, the problem is not too hard to fix...
unutbu
2010-09-02 11:09:57
list index out of range i am getting
alis
2010-09-02 12:10:17
@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
2010-09-02 12:34:44