如何用Python结合Google Search API统计搜索结果中关键词出现次数?
Alright, let's figure out how to count keyword occurrences in Google search results using Python—since you mentioned Google's official docs don't cover this directly, here's a practical approach:
We'll use Google's Custom Search JSON API to fetch structured search results (titles and snippets), then write Python logic to tally how many times your target keyword appears in those fields. This is the most reliable method (avoiding web scraping, which violates Google's terms and gets blocked easily).
First, you'll need two things:
- An API key from Google Cloud Console: Enable the Custom Search API in your project, then generate an API key.
- A Custom Search Engine (CX) ID: Create a new search engine (you can set it to search the entire web) via the Custom Search Engine portal, and copy the CX ID provided.
We'll use requests to call the API. Install it via pip:
pip install requests
Here's a complete script that fetches results, extracts titles/snippets, and counts your target keyword (we'll use "Mt. Everest" as an example):
import requests # Replace these with your actual credentials API_KEY = "YOUR_GOOGLE_CLOUD_API_KEY" CX_ID = "YOUR_CUSTOM_SEARCH_ENGINE_ID" SEARCH_TERM = "Mt. Everest" TARGET_KEYWORD = "Mt. Everest" # Construct the API request URL api_url = f"https://www.googleapis.com/customsearch/v1?q={SEARCH_TERM}&key={API_KEY}&cx={CX_ID}" # Send request and parse response response = requests.get(api_url) search_results = response.json() total_keyword_hits = 0 # Iterate through each result to count occurrences if "items" in search_results: for result in search_results["items"]: # Extract title and snippet (fallback to empty string if missing) result_title = result.get("title", "") result_snippet = result.get("snippet", "") # Count keyword (case-insensitive match—remove .lower() for exact case-sensitive count) title_count = result_title.lower().count(TARGET_KEYWORD.lower()) snippet_count = result_snippet.lower().count(TARGET_KEYWORD.lower()) # Print per-result stats print(f"Title: {result_title}") print(f"Keyword in title: {title_count} times") print(f"Keyword in snippet: {snippet_count} times\n") total_keyword_hits += title_count + snippet_count # Print final total print(f"Total occurrences of '{TARGET_KEYWORD}' across all results: {total_keyword_hits}")
- API Quotas: The free tier gives you 100 queries per day. If you need more, you'll have to enable billing (pricing is based on query volume).
- Pagination: The API returns 10 results per request. To get more, add the
startparameter (e.g.,&start=11to get results 11-20). - Text Preprocessing: For more accurate counts, you might want to clean the text first—remove punctuation, handle variations like "Mt Everest" vs "Mt. Everest", etc. For example, you could use regex to match the keyword regardless of trailing periods.
- Avoid Web Scraping: While you could use
BeautifulSoupto parse Google's search page, Google actively blocks scrapers, and this violates their Terms of Service. Stick to the official API for reliability.
内容的提问来源于stack exchange,提问作者Ali Anees




