Giter VIP home page Giter VIP logo

Comments (1)

dimitryzub avatar dimitryzub commented on May 28, 2024 1

A workaround of using pagination() with parse_qsl, urlsplit from urllib.parse:

from serpapi import GoogleSearch
from urllib.parse import (parse_qsl, urlsplit)

params = {
    'api_key': '...',     # serpapi api key
    'engine': 'google',   # search engine
    'q': 'minecraft',     # search query
    'hl': 'en'            # language
    # 'start': 0          # or explicitly add page number
}

search = GoogleSearch(params)

# iterate over all pages
while True:
    results = search.get_dict()

    print(f"Currenlty on page: {params.get('start')}")

    if 'error' in results:
        print(results['error'])
        break

    # for result in results.get('organic_results', []):
    #     print(result.get('position'), result.get('title'), sep='\n')

    # check if the next page key is present in the JSON
    # if present -> split URL in parts and update to the next page
    if 'next' in results.get('serpapi_pagination', {}):
        search.params_dict.update(dict(parse_qsl(urlsplit(results.get('serpapi_pagination').get('next')).query)))
    else:
        break

Output:

Currenlty on page: None # first page. if 'start' param is present and set explicitly to 0, None will become 0
Currenlty on page: 10   # second page
Currenlty on page: 20
Currenlty on page: 30
Currenlty on page: 40
Currenlty on page: 50
Currenlty on page: 60
Currenlty on page: 70

Example from the dashboard -> your searches page (Inspect last request (8th page), next hash key is empty thus exists pagination) :

image

image

from google-search-results-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.