I am using google search api But by default it shows 4 and maximum 8 results per page. I want more results per page.
Asked
Active
Viewed 880 times
1 Answers
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)
-
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