Provide a more convenient way to paginate via the Python package
See original GitHub issueCurrently, the way to paginate searches is to get the serpapi_pagination.current
and increase the offset
or start
parameters in the loop. Like with regular HTTP requests to serpapi.com/search
without an API wrapper.
import os
from serpapi import GoogleSearch
params = {
"engine": "google",
"q": "coffee",
"tbm": "nws",
"api_key": os.getenv("API_KEY"),
}
search = GoogleSearch(params)
results = search.get_dict()
print(f"Current page: {results['serpapi_pagination']['current']}")
for news_result in results["news_results"]:
print(f"Title: {news_result['title']}\nLink: {news_result['link']}\n")
while 'next' in results['serpapi_pagination']:
search.params_dict[
"start"] = results['serpapi_pagination']['current'] * 10
results = search.get_dict()
print(f"Current page: {results['serpapi_pagination']['current']}")
for news_result in results["news_results"]:
print(
f"Title: {news_result['title']}\nLink: {news_result['link']}\n"
)
A more convenient way for an official API wrapper would be to provide some function like search.paginate(callback: Callable)
which will properly calculate offset for the specific search engine and loop through pages until the end.
import os
from serpapi import GoogleSearch
def print_results(results):
print(f"Current page: {results['serpapi_pagination']['current']}")
for news_result in results["news_results"]:
print(f"Title: {news_result['title']}\nLink: {news_result['link']}\n")
params = {
"engine": "google",
"q": "coffee",
"tbm": "nws",
"api_key": os.getenv("API_KEY"),
}
search = GoogleSearch(params)
search.paginate(print_results)
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
A guide on how to deal with pagination via Python. - GitHub
This article covers everything you need to know about dealing with pagination using Python. By the end of this article, you will be...
Read more >paginate · PyPI
What is pagination? --------------------- This module helps dividing large lists of items into pages. The user is shown one page at a time...
Read more >How to Implement Pagination Using FastAPI in Python
To implement pagination, we need to install fastapi-pagination library. fastapi-pagination is a library that provides pagination feature for FastAPI ...
Read more >Python requests arguments/dealing with api pagination
The AL API's results are paginated, and I can't figure out how to move beyond the first page of the results. Here is...
Read more >Pagination for a User-Friendly Django App - Real Python
Investigating the Django paginator in the Django shell is an excellent way to understand how the Django paginator behaves. However, using ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Actually we do not need a callback because SerpApi backend does already offer proper pagination.
see commit: f9a470a9efd8c8956cb84597b4c33136670e3abb
thanks for your feedbacks!
@ilyazub code improved to handle missing “serpapi_pagination” field on the last page. @kikohs README updated with information on pagination support.
Library released on pypi