Skip to content

serpapi/serpapi-python

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

User guide

SerpApi-Python

SerpApi allows to scrape any search engine results. It's easy, fast, easy, feature rich, cost effective, scalable and reliable.

This Python 3 library is meant to scrape and parse results from all major search engines available world wide including Google, Bing, Baidu, Yandex, Yahoo, Ebay, Home depot, Apple and more using SerpApi. This is an open source project hosted under https://github.com/serpapi/serpapi-python.

SerpApi.com provides a script builder to get you started quickly.

Installation

SerpApi can be installed with pip.

$ python -m pip install serpapi

Quick start

The following example runs a search for "coffee" using your secret API key which you can find at SerpApi Dashboard page. The serpapi.search object handles all of the details of connection pooling and thread safety so that you don't have to:

import serpapi

client = serpapi.Client(
  api_key = "secret_api_key", # from serpapi.com
)

parameters = {
  "q": "coffee",
}

results = client.search(parameters)

print(results)

Advanced settings

SerpApi Client uses urllib3 under the hood.

Optionally, the HTTP connection can be tuned:

SerpApi's SLA is 99.95% successful searches: https://serpapi.com/faq/general/do-you-provide-sla-guarantees

client = serpapi.Client(
  retries = 5, # or urllib3.util.Retry(connect=5, read=2)
  timeout = 4.2,
  api_key = "secret_api_key", # from serpapi.com
)

For more details: urllib3 documentation

Basic example per search engines

Search Google Local Services

import serpapi
import pprint
import os

  def test_search_google_local_services(self):
client = serpapi.Client({
    'api_key': os.getenv("API_KEY"),
    'timeout': 61.1,
    'retries': 2
})
data = client.search({
    'engine': 'google_local_services',
    'q': 'Electrician', 
    'place_id': 'ChIJOwg_06VPwokRYv534QaPC8g', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['local_ads'])

test: tests/example_search_google_local_services.py
Documentation: https://serpapi.com/google-local-services-search-api

Search Baidu

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'baidu',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'q': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['organic_results'])

test: tests/example_search_baidu.py
Documentation: https://serpapi.com/baidu-search-api

Search Ebay

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'ebay',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    '_nkw': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['organic_results'])

test: tests/example_search_ebay.py
Documentation: https://serpapi.com/ebay-search-api

Search Apple App Store

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'apple_app_store',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'term': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['organic_results'])

test: tests/example_search_apple_app_store.py
Documentation: https://serpapi.com/apple-app-store-search-api

Search Google Product

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'google_product',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'q': 'coffee', 
    'product_id': '4172129135583325756', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['product_results'])

test: tests/example_search_google_product.py
Documentation: https://serpapi.com/google-product-search-api

Search Google Maps

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'google_maps',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'q': 'pizza', 
    'll': '@40.7455096,-74.0083012,15.1z', 
    'type': 'search', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['local_results'])

test: tests/example_search_google_maps.py
Documentation: https://serpapi.com/google-maps-search-api

Search Google Images

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'google',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'engine': 'google', 
    'tbm': 'isch', 
    'q': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['images_results'])

test: tests/example_search_google_images.py
Documentation: https://serpapi.com/google-images-search-api

Search Google Search

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'google',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'q': 'coffee', 
    'engine': 'google', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['organic_results'])

test: tests/example_search_google_search.py
Documentation: https://serpapi.com/google-search-search-api

Search Google Jobs

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'google_jobs',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'q': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['jobs_results'])

test: tests/example_search_google_jobs.py
Documentation: https://serpapi.com/google-jobs-search-api

Search Naver

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'naver',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'query': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['ads_results'])

test: tests/example_search_naver.py
Documentation: https://serpapi.com/naver-search-api

Search Google Scholar

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'google_scholar',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'q': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['organic_results'])

test: tests/example_search_google_scholar.py
Documentation: https://serpapi.com/google-scholar-search-api

Search Youtube

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'youtube',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'search_query': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['video_results'])

test: tests/example_search_youtube.py
Documentation: https://serpapi.com/youtube-search-api

Search Google Reverse Image

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'google_reverse_image',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'image_url': 'https://i.imgur.com/5bGzZi7.jpg', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['image_sizes'])

test: tests/example_search_google_reverse_image.py
Documentation: https://serpapi.com/google-reverse-image-search-api

Search Yahoo

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'yahoo',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'p': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['organic_results'])

test: tests/example_search_yahoo.py
Documentation: https://serpapi.com/yahoo-search-api

Search Duckduckgo

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'duckduckgo',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'q': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['organic_results'])

test: tests/example_search_duckduckgo.py
Documentation: https://serpapi.com/duckduckgo-search-api

Search Bing

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'bing',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'q': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['organic_results'])

test: tests/example_search_bing.py
Documentation: https://serpapi.com/bing-search-api

Search Google Autocomplete

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'google_autocomplete',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'q': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['suggestions'])

test: tests/example_search_google_autocomplete.py
Documentation: https://serpapi.com/google-autocomplete-search-api

Search Home Depot

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'home_depot',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'q': 'table', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['products'])

test: tests/example_search_home_depot.py
Documentation: https://serpapi.com/home-depot-search-api

Search Walmart

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'walmart',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'query': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['organic_results'])

test: tests/example_search_walmart.py
Documentation: https://serpapi.com/walmart-search-api

Search Google Events

import serpapi
import pprint
import os

client = serpapi.Client({
    'engine': 'google_events',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'q': 'coffee', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['events_results'])

test: tests/example_search_google_events.py
Documentation: https://serpapi.com/google-events-search-api

Search Google Play

import serpapi
import pprint
import os

  def test_search_google_play(self):
client = serpapi.Client({
    'engine': 'google_play',
    'api_key': os.getenv("API_KEY")
  })
data = client.search({
    'q': 'kite', 
    'store': 'apps', 
})
pp = pprint.PrettyPrinter(indent=2)
pp.pprint(data['organic_results'])

test: tests/example_search_google_play.py
Documentation: https://serpapi.com/google-play-search-api

Developer Guide

Key goals

  • High code quality
  • KISS principles
  • Brand centric instead of search engine based
    • No hard coded logic per search engine
  • Simple HTTP client (lightweight, reduced dependency)
    • No magic default values
    • Thread safe
  • Easy to extends
  • Defensive code style (raise custom exception)
  • TDD
  • Best API coding pratice per platform

Inspiration

The API design was inpired by the most popular Python packages.

Quality expectation

  • 0 lint issues using pylint make lint
  • 99% code coverage running make test
  • 100% test passing: make test

Client design: Class diagram

classDiagram
  CustomClient *-- Client
  HttpClient <-- Client
  HttpClient *-- urllib3
  HttpClient *-- ObjectDecoder

  class Client {
    engine: String
    api_key: String
    parameter: Hash
    search()
    html()
    location()
    search_archive()
    account()
  }

  class HttpClient {
    start()
    decode()
  }

  class urllib3 {
    request()
  }
Loading

JSON search() : Sequence diagram

sequenceDiagram
    Client->>SerpApi.com: search() : http request 
    SerpApi.com-->>SerpApi.com: query search engine
    SerpApi.com-->>SerpApi.com: parse HTML into JSON
    SerpApi.com-->>Client: JSON payload
    Client-->>Client: decode JSON into Dict
Loading

Build flow

This project is automated using a good old Makefile. To pipe-clean the project run this: make

Open Makefile for more details.