Skip to content

Commit f9a470a

Browse files
committed
add pagination wrapper
1 parent 808945b commit f9a470a

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

serpapi/pagination.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
# Paginate response in SearpApi
3+
class Pagination:
4+
def __init__(self, client, start = 0, end = 1000000000, page_size = 10):
5+
self.client = client
6+
self.start = start
7+
self.end = end
8+
self.page_size = page_size
9+
10+
def __iter__(self):
11+
return self
12+
13+
def __next__(self):
14+
# execute search
15+
self.client.params_dict['start'] = self.start
16+
result = self.client.get_dict()
17+
18+
# quit if no next page
19+
if not 'next' in result['serpapi_pagination']:
20+
raise StopIteration
21+
22+
# increment page
23+
self.start += self.page_size
24+
25+
# ends
26+
if self.start > self.end:
27+
raise StopIteration
28+
29+
return result

serpapi/serp_api_client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import requests
22
import json
3+
from serpapi.pagination import Pagination
34

45
GOOGLE_ENGINE = 'google'
56
BING_ENGINE = 'bing'
@@ -165,4 +166,9 @@ def get_location(self, q, limit = 5):
165166
self.params_dict["limit"] = limit
166167
buffer = self.get_results('/locations.json')
167168
return json.loads(buffer)
168-
169+
170+
def pagination(self, start = 0, end = 1000000000):
171+
"""Return:
172+
Generator to iterate the search results pagination
173+
"""
174+
return Pagination(self, start, end)

tests/test_example.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def test_async(self):
5858
print("execute async search: q = " + company)
5959
search.params_dict["q"] = company
6060
data = search.get_dict()
61+
if data is not None:
62+
print("oops data is empty for: " + company)
63+
continue
6164
print("add search to the queue where id: " + data['search_metadata']['id'])
6265
# add search to the search_queue
6366
search_queue.put(data)

tests/test_google_search.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ class TestSearchApi(unittest.TestCase):
1010
def setUp(self):
1111
GoogleSearch.SERP_API_KEY = os.getenv("API_KEY", "demo")
1212

13+
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
14+
def test_paginate(self):
15+
search = GoogleSearch({"q": "Coffee", "location": "Austin,Texas"})
16+
pages = search.pagination(0, 20)
17+
print("display generated")
18+
urls = []
19+
for result in pages:
20+
urls.append(result['serpapi_pagination']['next'])
21+
self.assertEqual(len(urls), 2)
22+
self.assertTrue("start=10" in urls[0])
23+
self.assertTrue("start=20" in urls[1])
24+
1325
@unittest.skipIf((os.getenv("API_KEY") == None), "no api_key provided")
1426
def test_get_json(self):
1527
search = GoogleSearch({"q": "Coffee", "location": "Austin,Texas"})

0 commit comments

Comments
 (0)