Coder Social home page Coder Social logo

serpapi / serpapi-python Goto Github PK

View Code? Open in Web Editor NEW
55.0 13.0 6.0 407 KB

a Python client library for SerpApi.

Home Page: https://pypi.org/project/serpapi/

License: MIT License

Python 100.00%
api-client python scraping-api serpapi api-wrapper google-search-api google-search-results requests api

serpapi-python's Introduction

SerpApi Python Library & Package

serpapi python library logo

Package

serpapi-python

This repository is the home of the soon–to–be official Python API wrapper for SerpApi. This serpapi module allows you to access search data in your Python application.

SerpApi supports Google, Google Maps, Google Shopping, Bing, Baidu, Yandex, Yahoo, eBay, App Stores, and more. Check out the documentation for a full list.

Installation

To install the serpapi package, simply run the following command:

$ pip install serpapi

Please note that this package is separate from the legacy serpapi module, which is available on PyPi as google-search-results. This package is maintained by SerpApi, and is the recommended way to access the SerpApi service from Python.

Usage

Let's start by searching for Coffee on Google:

>>> import serpapi
>>> s = serpapi.search(q="Coffee", engine="google", location="Austin, Texas", hl="en", gl="us")

The s variable now contains a SerpResults object, which acts just like a standard dictionary, with some convenient functions added on top.

Let's print the first result:

>>> s["organic_results"][0]["link"]
'https://en.wikipedia.org/wiki/Coffee'

Let's print the title of the first result, but in a more Pythonic way:

>>> s["organic_results"][0].get("title")
'Coffee - Wikipedia'

The SerpApi.com API Documentation contains a list of all the possible parameters that can be passed to the API.

Documentation

Documentation is available on Read the Docs.

Basic Examples in Python

Search Bing

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'bing',
    'q': 'coffee'
})

Search Baidu

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'baidu',
    'q': 'coffee',
})

Search Yahoo

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'yahoo',
    'p': 'coffee',
})

Search YouTube

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'youtube',
    'search_query': 'coffee',
})

Search Walmart

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'walmart',
    'query': 'coffee',
})

Search eBay

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'ebay',
    '_nkw': 'coffee',
})

Search Naver

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'naver',
    'query': 'coffee',
})

Search Home Depot

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'home_depot',
    'q': 'table',
})

Search Apple App Store

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'apple_app_store',
    'term': 'coffee',
})

Search DuckDuckGo

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'duckduckgo',
    'q': 'coffee',
})

Search Google

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google',
    'q': 'coffee'
})

Search Google Scholar

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_scholar',
    'q': 'coffee',
})

Search Google Autocomplete

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_autocomplete',
    'q': 'coffee',
})

Search Google Product

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_product',
    'q': 'coffee',
    'product_id': '4887235756540435899',
})

Search Google Reverse Image

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_reverse_image',
    'image_url': 'https://i.imgur.com/5bGzZi7.jpg',
    'max_results': '1',
})

Search Google Events

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_events',
    'q': 'coffee',
})

Search Google Local Services

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_local_services',
    'q': 'electrician',
    'data_cid': '6745062158417646970',
})

Search Google Maps

import os
import serpapi


client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_maps',
    'q': 'pizza',
    'll': '@40.7455096,-74.0083012,15.1z',
    'type': 'search',
})

Search Google Jobs

import os
import serpapi


client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_jobs',
    'q': 'coffee',
})

Search Google Play

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_play',
    'q': 'kite',
    'store': 'apps',
    'max_results': '2',
})

Search Google Images

import os
import serpapi

client = serpapi.Client(api_key=os.getenv("API_KEY"))
results = client.search({
    'engine': 'google_images',
    'tbm': 'isch',
    'q': 'coffee',
})

License

MIT License.

Contributing

Bug reports and pull requests are welcome on GitHub. Once dependencies are installed, you can run the tests with pytest.

serpapi-python's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

serpapi-python's Issues

Update your python sdk to work with python version higher than 3.7

Fortunately for me, another project requires that i use 3.7 so it works fine there.
But another project requires that I use 3.9, so stuff like GoogleSearch(params) doesnt work now.
Update it thank you

I get these errors all the time now:
ImportError: cannot import name 'GoogleSearch' from 'serpapi'

SerpAPI raises a 429 when plan limit is reached

Is there a reason why SerpAPI raises a 429 error when the plan limit is reached?

serpapi.exceptions.HTTPError: 429 Client Error: Too Many Requests for url: https://serpapi.com/search?engine=google_scholar&q=MY_QUERY&api_key=MY_API_KEY

I expected to either get "Error" when accessing the search_metadata.status field of the retrieved JSON (as mentioned in the docs) or at least a meaningful exception.

How to collect all results using BaiduSearch

Hello, I am testing SerpAPI with BaiduSearch function. How do I collect all results using the parameter 'rn' and 'pn'? If 'rn' is limited to 50 results, then how do I collect all results? Is there a way to feed in 'other pages' separately to collect all results?

from serpapi import BaiduSearch

params = {
  "api_key": "xxxxxxxxxxxx",
  "engine": "baidu",
  "q": "市民社会",
  "ct": "2",
  "rn": "200",
  "gpc": "stf=1356994860,1696155445|stftype=1",
  "q5": "intitle: '市民社会'",
  "q6": "site:zhihu.com",
  "pn": "20"
}

Under organic results search.get_dict()['organic_results'] there were only 10 results listed, so I don't think the parameter 'rn' is working properly. I am using Python 3.9, OS. I inserted the parameters because there were 200 results over 20 pages on baidu, and I wanted to get all the links.

Enforce `‎SerpResults.yield_pages` to start from the first page

Currently, ‎SerpResults.yield_pages skips the first page. Because of that, it's required to duplicate the results processing in the clients' code.

current_page = self
while current_page.next_page_url and current_page_count < max_pages:
current_page = current_page.next_page()
current_page_count += 1
yield current_page

Let's yield current_page before entering the pagination loop.

@kennethreitz @jvmvik

Trouble installing package serpapi in OS

Hello, I'm having trouble installing the package sperapi, I am on MacOS, using Python3.9

(base) surfer-172-29-61-245-hotspot:~ esthersong$ python --version
Python 3.9.13

(base) surfer-172-29-61-245-hotspot:~ esthersong$ pip3 install serpapi
ERROR: Could not find a version that satisfies the requirement serpapi (from versions: none)
ERROR: No matching distribution found for serpapi

(base) surfer-172-29-61-245-hotspot:~ esthersong$ pip --version
pip 22.2.2 from /Users/esthersong/opt/anaconda3/lib/python3.9/site-packages/pip (python 3.9)

I tried both pip install serpapi-python & pip3 install serpapi-python but all of them do not work?

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.