question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a more convenient way to paginate via the Python package

See original GitHub issue

Currently, 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)

@jvmvik @hartator What do you think?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jvmvikcommented, Apr 30, 2021

Actually we do not need a callback because SerpApi backend does already offer proper pagination.

# initialize the search
search = GoogleSearch({"q": "Coffee", "location": "Austin,Texas"})
# to get 2 pages
start = 0
end = 20
# create a python generator
pages = search.pagination(start, end)
print("display generated")
urls = []
# fetch one result per iteration of the for loop
for result in pages:
	urls.append(result['serpapi_pagination']['next'])

self.assertEqual(len(urls), 2)
self.assertTrue("start=10" in urls[0])
self.assertTrue("start=20" in urls[1])

see commit: f9a470a9efd8c8956cb84597b4c33136670e3abb

1reaction
jvmvikcommented, Jun 7, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found