A Comprehensive Coding Tutorial for Advanced SerpAPI Integration with Google Gemini-1.5-Flash for Advanced Analytics

In this tutorial, we demonstrate how to combine the power of SerpAPI’s Google search capabilities with Google’s Gemini-1.5-Flash model to create an advanced, end-to-end research and analysis workflow within a Google Colab notebook. By defining an AdvancedSerpAPI Python class, users gain access to enhanced search methods that cover general web results, news articles, and images, […] The post A Comprehensive Coding Tutorial for Advanced SerpAPI Integration with Google Gemini-1.5-Flash for Advanced Analytics appeared first on MarkTechPost.

Jun 7, 2025 - 02:40
 0
A Comprehensive Coding Tutorial for Advanced SerpAPI Integration with Google Gemini-1.5-Flash for Advanced Analytics

In this tutorial, we demonstrate how to combine the power of SerpAPI’s Google search capabilities with Google’s Gemini-1.5-Flash model to create an advanced, end-to-end research and analysis workflow within a Google Colab notebook. By defining an AdvancedSerpAPI Python class, users gain access to enhanced search methods that cover general web results, news articles, and images, while also leveraging Gemini to perform in-depth analyses of those results. The code provides specialized utilities for targeting Marktechpost tutorials, aggregating content across categories like LangChain, ChatGPT, and MLOps, and then synthesizing actionable insights using a carefully constructed prompt.

!pip install google-search-results langchain-community langchain-core google-generativeai -q


import os
import json
from serpapi import GoogleSearch
import google.generativeai as genai
from datetime import datetime

We install the required Python packages for SerpAPI searches, LangChain utilities, and Google’s Gemini SDK. The subsequent imports bring in standard modules (os, json, datetime) for environment configuration, JSON handling, and timestamps, as well as SerpAPI’s GoogleSearch class for making API calls and genai for interacting with the Gemini model.

SERPAPI_API_KEY = "Use Your API Key Here"  
GEMINI_API_KEY = "Use Your API Key Here"  


os.environ["SERPAPI_API_KEY"] = SERPAPI_API_KEY
genai.configure(api_key=GEMINI_API_KEY)

We assign placeholder strings for your SerpAPI and Gemini API keys, then set the SerpAPI key as an environment variable (so SerpAPI calls authenticate automatically) and configure the Gemini client with its API key so you can invoke the Gemini model.

class AdvancedSerpAPI:
    def __init__(self, serpapi_key, gemini_key):
        self.serpapi_key = serpapi_key
        self.gemini_model = genai.GenerativeModel('gemini-1.5-flash')
       
    def search_google(self, query, num_results=5, location="United States"):
        """Enhanced Google search with multiple parameters"""
        params = {
            "engine": "google",
            "q": query,
            "api_key": self.serpapi_key,
            "num": num_results,
            "location": location,
            "hl": "en",
            "gl": "us"
        }
       
        search = GoogleSearch(params)
        results = search.get_dict()
        return self.extract_search_results(results)
   
    def search_news(self, query, days_back=7):
        """Search for recent news articles"""
        params = {
            "engine": "google_news",
            "q": query,
            "api_key": self.serpapi_key,
            "gl": "us",
            "hl": "en"
        }
       
        search = GoogleSearch(params)
        results = search.get_dict()
        return self.extract_news_results(results)
   
    def search_images(self, query, num_images=10):
        """Search for images with metadata"""
        params = {
            "engine": "google_images",
            "q": query,
            "api_key": self.serpapi_key,
            "num": num_images
        }
       
        search = GoogleSearch(params)
        results = search.get_dict()
        return self.extract_image_results(results)
   
    def extract_search_results(self, results):
        """Extract and clean search results"""
        cleaned_results = []
        if 'organic_results' in results:
            for result in results['organic_results']:
                cleaned_results.append({
                    'title': result.get('title', ''),
                    'link': result.get('link', ''),
                    'snippet': result.get('snippet', ''),
                    'position': result.get('position', 0)
                })
        return cleaned_results
   
    def extract_news_results(self, results):
        """Extract news articles with timestamps"""
        news_results = []
        if 'news_results' in results:
            for article in results['news_results']:
                news_results.append({
                    'title': article.get('title', ''),
                    'link': article.get('link', ''),
                    'snippet': article.get('snippet', ''),
                    'date': article.get('date', ''),
                    'source': article.get('source', '')
                })
        return news_results
   
    def extract_image_results(self, results):
        """Extract image results with metadata"""
        image_results = []
        if 'images_results' in results:
            for img in results['images_results']:
                image_results.append({
                    'title': img.get('title', ''),
                    'original': img.get('original', ''),
                    'thumbnail': img.get('thumbnail', ''),
                    'source': img.get('source', '')
                })
        return image_results
   
    def analyze_with_gemini(self, search_results, analysis_prompt):
        """Use Gemini Flash to analyze search results"""
        results_text = json.dumps(search_results, indent=2)
       
        full_prompt = f"""
        {analysis_prompt}
       
        Search Results Data:
        {results_text}
       
        Please provide a comprehensive analysis based on the search results.
        """
       
        try:
            response = self.gemini_model.generate_content(full_prompt)
            return response.text
        except Exception as e:
            return f"Gemini analysis failed: {str(e)}"
   
    def search_marktechpost_tutorials(self, topic="", num_results=10):
        """Search specifically for trending tutorials from Marktechpost"""
        queries = [
            f"site:marktechpost.com {topic} tutorial guide how-to 2024 2025",
            f"site:marktechpost.com trending {topic} tutorial",
            f"site:marktechpost.com top {topic} books frameworks"
        ]
       
        all_results = []
        for query in queries:
            params = {
                "engine": "google",
                "q": query,
                "api_key": self.serpapi_key,
                "num": num_results // len(queries),
                "hl": "en",
                "gl": "us"
            }
           
            search = GoogleSearch(params)
            results = search.get_dict()
            extracted = self.extract_search_results(results)
            all_results.extend(extracted)
       
        unique_results = []
        seen_links = set()
        for result in all_results:
            if result['link'] not in seen_links:
                unique_results.append(result)
                seen_links.add(result['link'])
       
        return unique_results[:num_results]
   
    def get_trending_marktechpost_content(self, categories=None):
        """Get trending content from Marktechpost across different categories"""
        if categories is None:
            categories = ["AI", "LLM", "Machine Learning", "Python", "Tutorial", "Framework"]
       
        trending_content = {}
       
        for category in categories:
            print(f"                        </div>
                                            <div class=
                            
                                Read More