Google Search Python Library
googlesearch is a Python library for scraping the Google search engine. It uses `requests` and `BeautifulSoup4` to parse Google's search results. The library is actively maintained with releases addressing changes in Google's search page structure. The current version is 1.3.0, released January 21, 2025.
Warnings
- breaking Google frequently updates its search page's HTML structure, which can break the library's scraping logic, leading to empty results or parsing failures.
- gotcha Automated searches are often detected and throttled or blocked by Google, leading to HTTP 429 (Too Many Requests) errors, captchas, or empty results.
- gotcha There are multiple Python packages with similar names (`google`, `python-googlesearch`, `googlesearch-python`), leading to `ModuleNotFoundError` if the wrong one is installed or imported.
- gotcha By default, the `search()` function yields only URLs. Users expecting titles or descriptions will find these missing from the results.
Install
-
pip install googlesearch-python
Imports
- search
from googlesearch import search
Quickstart
from googlesearch import search
query = "latest Python news"
# The 'pause' parameter helps avoid IP blocks from Google by adding a delay between requests.
# 'num_results' specifies how many unique URLs to return.
results = list(search(query, num_results=5, lang="en", pause=2))
if results:
print(f"Top 5 results for '{query}':")
for i, url in enumerate(results):
print(f"{i+1}. {url}")
else:
print(f"No results found for '{query}'.")
# To get more detailed information (title, URL, description), use 'advanced=True'
# from googlesearch import search, SearchResult
# adv_results = list(search(query, advanced=True, num_results=3, pause=2))
# if adv_results:
# print("\nAdvanced search results:")
# for i, res in enumerate(adv_results):
# print(f"{i+1}. Title: {res.title}")
# print(f" URL: {res.url}")
# print(f" Description: {res.description}")