Google Search Results via SERP API pip Python Package
Google Search Results via SERP API pip Python Package
[!WARNING] This package will soon be deprecated in favor of serpapi-python. We recommend migrating to the newer implementation to ensure continued support and access to the latest features and improvements.
This Python package is meant to scrape and parse search results from Google, Bing, Baidu, Yandex, Yahoo, Home Depot, eBay and more, using SerpApi.
The following services are provided:
SerpApi provides a script builder to get you started quickly.
Python 3.7+
pip install google-search-resultsLink to the python package page
from serpapi import GoogleSearch
search = GoogleSearch({
"q": "coffee",
"location": "Austin,Texas",
"api_key": ""
})
result = search.get_dict()This example runs a search for "coffee" using your secret API key.
The SerpApi service (backend)
Et voilà...
Alternatively, you can search:
See the playground to generate your code.
Source code.
…Link to the full documentation
See below for more hands-on examples.
You can get an API key here if you don't already have one: https://serpapi.com/users/sign_up
The SerpApi api_key can be set globally:
GoogleSearch.SERP_API_KEY = "Your Private Key"The SerpApi api_key can be provided for each search:
query = GoogleSearch({"q": "coffee", "serp_api_key": "Your Private Key"})We love true open source, continuous integration and Test Driven Development (TDD). We are using RSpec to test our infrastructure around the clock to achieve the best Quality of Service (QoS).
The directory test/ includes specification/examples.
Set your API key.
export API_KEY="your secret key"Run test
make testfrom serpapi import GoogleSearch
search = GoogleSearch({})
location_list = search.get_location("Austin", 3)
print(location_list)This prints the first 3 locations matching Austin (Texas, Texas, Rochester).
[ { 'canonical_name': 'Austin,TX,Texas,United States',
'country_code': 'US',
'google_id': 200635,
'google_parent_id': 21176,
'gps': [-97.7430608, 30.267153],
'id': '585069bdee19ad271e9bc072',
'keys': ['austin', 'tx', 'texas', 'united', 'states'],
'name': 'Austin, TX',
'reach': 5560000,
'target_type': 'DMA Region'},
...]The search results are stored in a temporary cache. The previous search can be retrieved from the cache for free.
from serpapi import GoogleSearch
search = GoogleSearch({"q": "Coffee", "location": "Austin,Texas"})
search_result = search.get_dictionary()
assert search_result.get("error") == None
search_id = search_result.get("search_metadata").get("id")
print(search_id)Now let's retrieve the previous search from the archive.
archived_search_result = GoogleSearch({}).get_search_archive(search_id, 'json')
print(archived_search_result.get("search_metadata").get("id"))This prints the search result from the archive.
from serpapi import GoogleSearch
search = GoogleSearch({})
account = search.get_account()This prints your account information.
from serpapi import BingSearch
search = BingSearch({"q": "Coffee", "location": "Austin,Texas"})
data = search.get_dict()This code prints Bing search results for coffee as a Dictionary.
https://serpapi.com/bing-search-api
from serpapi import BaiduSearch
search = BaiduSearch({"q": "Coffee"})
data = search.get_dict()This code prints Baidu search results for coffee as a Dictionary. https://serpapi.com/baidu-search-api
from serpapi import YandexSearch
search = YandexSearch({"text": "Coffee"})
data = search.get_dict()This code prints Yandex search results for coffee as a Dictionary.
https://serpapi.com/yandex-search-api
from serpapi import YahooSearch
search = YahooSearch({"p": "Coffee"})
data = search.get_dict()This code prints Yahoo search results for coffee as a Dictionary.
https://serpapi.com/yahoo-search-api
from serpapi import EbaySearch
search = EbaySearch({"_nkw": "Coffee"})
data = search.get_dict()This code prints eBay search results for coffee as a Dictionary.
https://serpapi.com/ebay-search-api
from serpapi import HomeDepotSearch
search = HomeDepotSearch({"q": "chair"})
data = search.get_dict()This code prints Home Depot search results for chair as Dictionary.
https://serpapi.com/home-depot-search-api
from serpapi import YoutubeSearch
search = YoutubeSearch({"q": "chair"})
data = search.get_dict()This code prints Youtube search results for chair as Dictionary.
https://serpapi.com/youtube-search-api
from serpapi import GoogleScholarSearch
search = GoogleScholarSearch({"q": "Coffee"})
data = search.get_dict()This code prints Google Scholar search results.
from serpapi import WalmartSearch
search = WalmartSearch({"query": "chair"})
data = search.get_dict()This code prints Walmart search results.
from serpapi import YoutubeSearch
search = YoutubeSearch({"search_query": "chair"})
data = search.get_dict()This code prints Youtube search results.
from serpapi import AppleAppStoreSearch
search = AppleAppStoreSearch({"term": "Coffee"})
data = search.get_dict()This code prints Apple App Store search results.
from serpapi import NaverSearch
search = NaverSearch({"query": "chair"})
data = search.get_dict()This code prints Naver search results.
from serpapi import SerpApiClient
query = {"q": "Coffee", "location": "Austin,Texas", "engine": "google"}
search = SerpApiClient(query)
data = search.get_dict()This class enables interaction with any search engine supported by SerpApi.com
from serpapi import GoogleSearch
search = GoogleSearch({"q": "coffe", "tbm": "isch"})
for image_result in search.get_dict()['images_results']:
link = image_result["original"]
try:
print("link: " + link)
# wget.download(link, '.')
except:
passThis code prints all the image links, and downloads the images if you un-comment the line with wget (Linux/OS X tool to download files).
This tutorial covers more ground on this topic. https://github.com/serpapi/showcase-serpapi-tensorflow-keras-image-training
from serpapi import GoogleSearch
search = GoogleSearch({
"q": "coffe", # search search
"tbm": "nws", # news
"tbs": "qdr:d", # last 24h
"num": 10
})
for offset in [0,1,2]:
search.params_dict["start"] = offset * 10
data = search.get_dict()
for news_result in data['news_results']:
print(str(news_result['position'] + offset * 10) + " - " + news_result['title'])This script prints the first 3 pages of the news headlines for the last 24 hours.
from serpapi import GoogleSearch
search = GoogleSearch({
"q": "coffe", # search search
"tbm": "shop", # shopping
"tbs": "p_ord:rv", # ordered by review
"num": 100
})
data = search.get_dict()
for shopping_result in data['shopping_results']:
print(shopping_result['position']) + " - " + shopping_result['title'])
This script prints all the shopping results, ordered by review order.
With SerpApi, we can build a Google search from anywhere in the world. This code looks for the best coffee shop for the given cities.
from serpapi import GoogleSearch
for city in ["new york", "paris", "berlin"]:
location = GoogleSearch({}).get_location(city, 1)[0]["canonical_name"]
search = GoogleSearch({
"q": "best coffee shop", # search search
"location": location,
"num": 1,
"start": 0
})
data = search.get_dict()
top_result = data["organic_results"][0]["title"]We offer two ways to boost your searches thanks to theasync parameter.
…This code shows how to run searches asynchronously. The search parameters must have {async: True}. This indicates that the client shouldn't wait for the search to be completed. The current thread that executes the search is now non-blocking, which allows it to execute thousands of searches in seconds. The SerpApi backend will do the processing work. The actual search result is deferred to a later call from the search archive using get_search_archive(search_id). In this example the non-blocking searches are persisted in a queue: search_queue. A loop through the search_queue allows it to fetch individual search results. This process can easily be multithreaded to allow a large number of concurrent
No open issues yet, or sync has not completed.