惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

SecWiki News
SecWiki News
M
MIT News - Artificial intelligence
博客园 - 司徒正美
I
InfoQ
V
V2EX
L
LangChain Blog
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
H
Help Net Security
美团技术团队
Y
Y Combinator Blog
G
Google Developers Blog
小众软件
小众软件
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
Jina AI
Jina AI
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Spread Privacy
Spread Privacy
博客园 - 聂微东
The Register - Security
The Register - Security
F
Full Disclosure
S
Securelist
G
GRAHAM CLULEY
Cyberwarzone
Cyberwarzone
F
Fox-IT International blog
H
Hacker News: Front Page
C
Cisco Blogs
D
Docker
L
LINUX DO - 热门话题
Google Online Security Blog
Google Online Security Blog
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
ThreatConnect
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
J
Java Code Geeks
宝玉的分享
宝玉的分享
Project Zero
Project Zero
L
LINUX DO - 最新话题
博客园_首页
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
博客园 - 叶小钗

DEV Community

Great example of Gemma 4 moving beyond chatbots into real-world decision support. Using AI to guide everyday actions like recycling shows how impactful applied LLMs can be when designed for usability, not just capability. #Gemma4 #AI #Sustainability Building a Production AI Chatbot for an Educational Institute: Architecture, Lessons & Full Stack Deep-Dive Google Login in Express with PassportJS & JWT How I reclaimed 47GB on my MacBook by cleaning developer project junk Operators Are Not Oracles: How We Learned to Stop Worrying and Love the Configuration I Built 6 Free Developer Tools for AI APIs, Cron, Docker, and Self-Hosting How I Built a Real-Time Precious Metals Price Feed for 30,000 Concurrent Users in Laravel Gemma 4 discussions often focus on capability, but real-world impact depends on deployment context. For offline education, especially in low-connectivity regions, latency, cost, and local inference matter as much as model strength. Local Mind Explores it Space Complexity + Ω and Θ Notations Google I/O 2026 Just Confirmed the Shift From AI Chatbots to AI Agents How to Add API Monitoring to an Express App in 5 Minutes (2026) Designing an In-Game Inflation Tracking Algorithm for Web Utility Apps Google AI Studio Just Changed the Shape of App Development If you struggle to learn then this is for you. Best AI Agent Security & Guardrails Tools in 2026: LLM Guard vs NeMo vs Guardrails AI Building Dynamic RBAC in React 19: From Permission Strings to Component-Level Access Control How to Build a Self-Hosted AI Code Review Tool in Python Why We Switched from React to HTMX in Production: A 200-Site Case Study Gemma-Loom: The Intent-Based Virtual Machine (IVM) for Edge Sovereignty Java实习海投攻略:3天300个沟通,我是怎么拿到面试的 I Deployed Netflix's Web Server in 30 Seconds (And So Can You) - Docker Project 1 Debugging Android 14 WebRTC Disconnects on a coturn Relay Path 1/30 Days System Design Question Testing FastAPI + SQLAlchemy with Real PostgreSQL Fixtures: No More Mocking Misery FAQ Schema Markup Generators: What They Actually Do (and What They Don't Tell You) How a pure-TypeScript flex layout engine closed the last WASM-Yoga gap Spot instances as GitHub Actions runners Agents Need Receipts, Not Just Better Prompts readmegen — Generate beautiful README.md in seconds (12 templates, open source) When AI Reads Blueprints: The Hidden Attack Surface of Multimodal Engineering Intelligence Simplicity scales — complexity kills side projects AI does exactly what you ask — that's the problem How a model upgrade silently broke our extraction prompt (and how we caught it) The Best Form Backend for Static Sites in 2026 # ⛽ I Built a Cross-Platform Fuel Finder with React & Supabase: The Indie Dev Journey The 11 Major Cloud Service Providers in 2025 Membangun Karya Visual: Mengintip Fasilitas Multimedia dan Studio Kreatif Amikom What Is IOPS? Visualizing Database Design: From Interactive Canvas to Drizzle, Prisma, and SQL in Real-time A tool to make your GitHub README impossible to ignore 🚀 Zero-Downtime Blue-Green and IP-Based Canary Deployments on ECS Fargate I reproduced a Claude Code RCE. The bug pattern is everywhere. We Replaced Our RAG Pipeline With Persistent KV Cache. Here's What We Found. Jenkins CI/CD Pipeline for a Dockerized Node.js Application: Manual Trigger vs Automatic Trigger Using GitHub Webhooks How to Stream Live Forex Rates to Google Sheets API: A Complete Guide Small Models Will Beat Giant Models (And Most People Haven’t Realized Why Yet) How I Built 5 Linux Automation Scripts on AWS EC2 I built TokenPatch to measure AI coding cost per applied patch I built a Chrome extension to stop squinting at the web Producer audit clean, six tests red Conversa — A Multi-Agent AI Platform Powered by Gemma 4 Build a Real Agent in 15 Minutes with Gemini's New Managed Agents API What I Actually Build: AI Systems That Ship, Not Demos That Impress The Box Ticked While You Read This: LinkedIn, AI Training, and the Switch You Did Not Flip Investasi Masa Depan: Mengintip Fasilitas Laboratorium Komputer Kelas Dunia di Yogyakarta I Cancelled My $20 Claude Cowork Plan After a Week With OpenWork Stop Reviewing Every Line of AI Code - Build the Trust Stack Instead How To Build an Image Cropper in Browser (Simple Steps) I built a macOS disk cleaner for developers and just launched it would love feedback Membangun Kompetensi dan Relasi: Mengapa Ekosistem Kampus Itu Penting I Built an AI That Decides Which AI to Talk To — Running 24/7 From My Living Room Codex Team Usage SOP How to Actually Become a Programmer: The Hard Part Nobody Wants to Explain Building a Production-Style Multi-Tool AI Agent with Python, Flask, React & Gemini AI The Caretaker Sandbox: An Offline-First Visual Playground & Template Engine powered by Gemma 4 # Building Instagram OSINT Projects with HikerAPI Your AI can read. Gemma 4 can see The Battle of the Senior Dev: Why AI Gives You Wings But Only If You're Ready to Pilot HiDream Raw Output Failed Tried Dev-2604 VRAM Math Killed It Won with a Prompt Enhancer Instead I Finally Finished a Project I Abandoned — And GitHub Copilot Helped Me Ship It SafeSMS: On-Device Threat Detection with Gemma 4 E4B, no internet required I Built OpenKap — A Loom Alternative for Small Teams Who Just Want to Ship Gemma 4 is Here: The Dawn of Local Multimodal Reasoning Offline-First Flutter: How We Built a CRM That Manages 100K+ Leads With No Internet Memory for Agents: When Vectors Meet Graphs, Bugs Drop 4 The Rise of Production-Grade AI Infrastructure I ran my idea-validation product through its own validator. The verdict was PIVOT. We Built an Agent Commerce API. Google I/O 2026 Changed Our 3-Month Roadmap in 24 Hours. "My Partner's Memory Was Full. I Didn't Know — Until We Tried to Talk." I’m a Front End Web Developer Learning Machine Learning From Scratch Laravel Waiting Request I Built a Chrome Extension to Track How Long You Actually Spend on Each Tab Why Google Can't See Your React Breadcrumbs (And the 4-Line Fix) AI Travel Assistant Powered by Gemma 4; With Streaming, Image Input, and Visual Recommendation Cards Microsoft tried to kill the printer driver. Healthcare said no. The Blueprint Beneath the Blueprint: Designing Data Model and Choosing Its Database REST APIs vs Webhooks in Telecom Billing - Which One Actually Makes Sense? Accounting Made Simple: AI-Powered Financial Insights of Japanese Companies with Gemma 4 The append-only AST trick that makes Flutter AI chat actually smooth Designing the Future of Payments — Why XML Still Matters in the Age of APIs From Legacy to Live — Reviving XMLPayments with GitHub Copilot Two Weeks Into Learning Solana XMLPayments — The Hidden Backbone of Modern Financial Orchestration AI Agents in Practice — Read from the beginning Reviving My Gemma Agentic Framework: From Prototype to Polished Repo Smart Contracts Demand Better Infrastructure: Building on contract.dev Self-Hosted LLM Tool Calling: Forge and the Build-vs-Buy Decision ORA-00072 오류 원인과 해결 방법 완벽 가이드 OpenWA for CTOs: Self-Hosted WhatsApp Gateway Trade-Offs NotebookLM Automation With notebooklm-py: Useful, But Classify Data First
How to Use a SERP API to Validate Whether a Project Idea Is Worth Building
Elowen · 2026-05-23 · via DEV Community

You may have an idea: building an AI resume builder, a PDF tool site, a small SaaS product, a content site, an affiliate site, a niche directory, or even a local service website. The first question is simple: is this direction actually worth building?

Many people judge by instinct: this niche seems popular, there seem to be many existing sites, and users probably need it. But before you start building, a more reliable approach is to look at search results. Search results already contain user demand, page types, content formats, and commercial signals.

This article walks through a complete workflow: starting from a project idea, using the TalorData SERP API to collect real search results, turning those results into tables, and then deciding whether the direction has an opportunity.

We will use AI Resume Builder as the example. The same method can also be applied to tool sites, SaaS products, content sites, affiliate sites, directories, and local service websites.

What You Will Get at the End

By following this article, you will produce three tables:

  • Keyword candidate table: turn a project idea into a set of searchable keywords.
  • SERP result table: use the SERP API to check each keyword and collect ranking pages, PAA, ads, domains, and other useful signals.
  • Opportunity decision table: use simple rules to decide which keywords are worth targeting and which ones should be postponed or avoided.

At the end, you should be able to answer three questions:

  • Is there real search demand for this direction?
  • Which sites are already ranking in search results?
  • If I build this now, which keyword or feature should I start with?

Step 1: Turn the Idea into Searchable Keywords

Let's start with a concrete idea: I want to build an AI Resume Builder.

This idea is still too broad. You cannot judge the whole project with only one phrase, because a project usually contains many different user needs. Some users want a free tool. Some want an online resume builder. Some want a cover letter generator. Some only need a resume summary generator.

So the first step is not writing code or calling an API. The first step is turning the idea into search queries that real users might type into Google.

Step 2: Where the Keywords Come From

Keywords should not be guessed randomly. You can get the first batch from several sources.

1. Break Down the Core Features

For an AI Resume Builder, you can start with the core features:

  • Resume generation: ai resume builder
  • Online resume tool: resume builder online
  • Free resume tool: free resume builder
  • Resume summary generation: resume summary generator
  • Cover letter generation: cover letter generator
  • Resume bullet point generation: resume bullet point generator

2. Expand with User Modifiers

When users search for tools, they often add modifiers such as:

  • best, for example best resume builder
  • free, for example free resume builder
  • online, for example resume builder online
  • generator, for example resume summary generator
  • template, for example resume template
  • alternative, for example zety alternative

3. Add Keywords from Search Suggestions and Existing Pages

You can also expand keywords from Google autocomplete, People Also Ask, existing page titles, and discussions on Reddit or Quora.

If you do not know where to start, you can use ChatGPT to generate an initial keyword list, then use a SERP API to verify whether those keywords actually have search results and ranking pages.

Step 3: Create keywords.csv

Put the keywords into a CSV file. Keep the first version simple, with only one column: keyword.

keyword
ai resume builder
resume builder online
free resume builder
resume summary generator
cover letter generator
resume bullet point generator
ai cover letter generator
best resume builder

Enter fullscreen mode Exit fullscreen mode

If you are testing a different project, just replace this column. For example:

  • PDF tool site: pdf compressor online, merge pdf online, pdf to word converter
  • SaaS: email verification API, invoice software, booking software
  • Content site: best dog food for puppies, how to train a puppy
  • Local service site: plumber near me, emergency dentist near me

Step 4: Query Keywords with the TalorData SERP API

Next, send one SERP API request for each keyword. You can register a TalorData account and get an API key with free credits from the official website.

Keep in mind that API requests consume quota. In general, one keyword means at least one request. If you prepare 20 keywords, the test will use around 20 requests. Before expanding the test, start with 5-10 keywords to make sure the workflow works.

The basic TalorData request format looks like this:

curl -X POST 'https://serpapi.talordata.net/serp/v1/request' \
  -H 'Authorization: Bearer YOUR_TALORDATA_API_KEY' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'engine=google' \
  -d 'q=ai resume builder' \
  -d 'json=2'

Enter fullscreen mode Exit fullscreen mode

This request returns Google SERP data for the keyword. For decision-making, we mainly care about these fields:

  • organic results: natural search results, used to see who ranks at the top.
  • People Also Ask: user questions, used to identify content opportunities.
  • ads: advertising results, if available, used to estimate commercial value.
  • titles and URLs: used to understand page types.
  • domains: used to identify and count the sites that appear in search results.

Step 5: Run the Script and Generate the Result Table

The script reads keywords.csv, queries the TalorData SERP API, and outputs two files:

  • serp_opportunities.csv: the analysis table used for judging opportunities.
  • serp_raw_records.jsonl: the raw crawl records, useful for checking how each conclusion was produced.

In the main article, we only show the key part of the code:

TALORDATA_API_KEY = "YOUR_TALORDATA_API_KEY_HERE"
ENDPOINT = "https://serpapi.talordata.net/serp/v1/request"

headers = {
    "Authorization": f"Bearer {TALORDATA_API_KEY}",
    "Content-Type": "application/x-www-form-urlencoded",
    "User-Agent": "curl/8.0.0",
}

data = {
    "engine": "google",
    "q": keyword,
    "json": "2",
}

response = requests.post(ENDPOINT, headers=headers, data=data, timeout=60)
response.raise_for_status()
result = response.json()

Enter fullscreen mode Exit fullscreen mode

This is the part that sends a keyword to TalorData and receives the SERP response. The full script is included at the end of the article.

Step 6: Look at the Result Table Before Judging the Opportunity

The script will produce a table similar to this:

keyword organic_count paa_count ads_present top_domains Initial read
ai resume builder 10 4 true resume.io, resumegenius.com, enhancv.com Demand is clear, but competition is strong
free resume builder 10 5 true canva.com, zety.com, resume.com Strong free-tool demand and obvious commercial competition
resume summary generator 9 6 false tealhq.com, resumeworded.com, kickresume.com Better long-tail opportunity
cover letter generator 10 4 true grammarly.com, resumegenius.com, zety.com Suitable as a feature page entry point

This table is your first project opportunity map. It shows demand strength, ranking-site patterns, and commercial signals behind each keyword.

Step 7: How to Decide Whether a Signal Is Strong or Weak

When judging opportunities, avoid vague words like "many", "few", "strong", or "weak". Use simple rules instead.

Signal How to Judge What It Means
PAA >= 4 Many questions Rich user needs; suitable for content pages or FAQ
PAA 1-3 Some questions Some content opportunity
PAA = 0 Few questions Limited room for content expansion
ads_present = true Ads exist The keyword may have commercial value
Top 10 has 2+ small sites Better new-site opportunity The SERP is not fully dominated by major brands
Top 10 is almost all major sites Strong competition New sites should avoid attacking the core keyword directly

Here, major sites mean strong brands, platforms, or high-authority websites, such as Canva, LinkedIn, Indeed, HubSpot, Forbes, and Wikipedia.

Small sites usually mean niche tool sites, small SaaS products, personal blogs, niche content sites, or emerging product sites.

You should also keep clear "give up" signals. If almost every keyword has a Top 10 dominated by major sites, very few PAA questions, no ads, and no small sites ranking, the direction may have weak demand or may already be heavily monopolized. In that case, do not force the conclusion that it is still worth doing. A better move is to switch to a more specific keyword or choose another direction.

Step 8: Turn the Result into a Conclusion

Now let's go back to the AI Resume Builder example. Based on the sample results, we can make these judgments:

  • ai resume builder: strong demand, but also strong competition. It is not a good first breakthrough keyword.
  • free resume builder: high traffic and commercial value, but many major brands compete for it.
  • resume summary generator: more specific, with many PAA questions, suitable as an early entry point.
  • cover letter generator: clear demand, suitable as a second feature page.

So the direction is not "not worth doing". The real conclusion is: do not start by attacking the hardest core keyword. A more practical starting point is to build specific feature pages first, then expand toward the main tool page.

Step 9: Create Your Opportunity Decision Table

Finally, convert each keyword into a clear decision.

keyword Opportunity Level Suggested Action
ai resume builder Medium Treat it as a long-term target, not the first keyword to attack
free resume builder Medium Has traffic and commercial value, but needs differentiation
resume summary generator High Suitable as one of the first feature pages
cover letter generator High Suitable as a feature expansion page
best resume builder Medium-low Better for later comparison content

The goal is not to produce an absolutely correct answer. The goal is to decide where to put resources first: which pages to build now, which keywords to postpone, and which directions are not worth continuing.

Optional: Turn the Rules into a Simple Score

If you want more automation, you can turn the rules from Step 7 into a simple scoring function. This lets the script output an initial score for each keyword.

def score_keyword(paa_count, ads_present, big_site_count, small_site_count):
    score = 0

    if paa_count >= 4:
        score += 2
    elif paa_count >= 1:
        score += 1

    if ads_present:
        score += 2

    if small_site_count >= 2:
        score += 2

    if big_site_count >= 4:
        score -= 2

    if score >= 4:
        return "High"
    if score >= 2:
        return "Medium"
    return "Low"

Enter fullscreen mode Exit fullscreen mode

This score should not replace human judgment, but it can help you quickly identify which keywords deserve attention first.

Step 10: Apply the Method to Your Own Project

This method can be reused across many project types. You only need to replace the keywords.

Project Type Example Keyword What You Can Validate
Tool site pdf compressor online Tool demand, existing tools, free/paid opportunity
SaaS email verification API Commercial intent, SaaS sites, pricing-page competition
Content site best dog food for puppies Content opportunity, PAA questions, affiliate opportunity
Directory site best AI tools for students Listicle opportunity and existing directory sites
Local service site plumber near me Local competition, local pack, service provider rankings

The general workflow is:

Project idea -> Find keywords -> Query with TalorData SERP API -> Get result table -> Judge opportunity by rules -> Decide whether to build and where to start

Enter fullscreen mode Exit fullscreen mode

Conclusion

A project direction should not be judged only by instinct. You need to look at real search results to understand demand, commercial value, existing ranking sites, and possible entry points.

The value of a SERP API is that it turns Google search results into data you can analyze in batches.

For a new project, the best direction is not always the most popular one. A better direction is one with search demand, commercial value, room for content or feature expansion, and a realistic entry point for a new site.

If you already have an idea, start with 5-10 keywords and run this workflow once. You will have a much clearer view of whether the direction is worth building and where your first step should be.

Full Python script

# -*- coding: utf-8 -*-
import csv
import json
from datetime import datetime
from pathlib import Path
from urllib.parse import urlparse

import requests


ROOT = Path(__file__).resolve().parent
ENDPOINT = "https://serpapi.talordata.net/serp/v1/request"
TALORDATA_API_KEY = "YOUR_TALORDATA_API_KEY_HERE"
SERP_PAGE = ""
SERP_NUM = ""


def get_domain(url):
    try:
        return urlparse(url).netloc.replace("www.", "")
    except Exception:
        return ""


def normalize_serp_response(result):
    serp_json = result.get("data", {}).get("json", result)
    if isinstance(serp_json, str):
        return json.loads(serp_json)
    return serp_json


def find_first(data, keys):
    if isinstance(data, dict):
        for key in keys:
            if key in data:
                return data[key]
        for value in data.values():
            found = find_first(value, keys)
            if found is not None:
                return found
    elif isinstance(data, list):
        for item in data:
            found = find_first(item, keys)
            if found is not None:
                return found
    return None


def as_list(value):
    if isinstance(value, list):
        return value
    if isinstance(value, dict):
        return list(value.values())
    return []


def count_any(data, keys):
    value = find_first(data, keys)
    return len(as_list(value))


def fetch_serp(keyword, api_key):
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/x-www-form-urlencoded",
        "User-Agent": "curl/8.0.0",
        "Accept": "application/json, text/plain, */*",
    }
    data = {
        "engine": "google",
        "q": keyword,
        "json": "2",
    }
    page = SERP_PAGE.strip()
    num = SERP_NUM.strip()
    if page:
        data["page"] = page
    if num:
        data["num"] = num
    response = requests.post(ENDPOINT, headers=headers, data=data, timeout=60)
    response.raise_for_status()
    return normalize_serp_response(response.json())


def classify_opportunity(paa_count, ads_present, top_domains):
    big_brand_markers = (
        "linkedin.com",
        "indeed.com",
        "canva.com",
        "wikipedia.org",
        "forbes.com",
        "hubspot.com",
    )
    big_site_count = sum(
        1 for domain in top_domains if any(marker in domain for marker in big_brand_markers)
    )
    if paa_count >= 4 and big_site_count <= 2:
        return "High", "Many PAA questions; top results aren’t fully dominated by major sites."
    if ads_present and paa_count >= 3:
        return "Medium", "There is commercial value and some content opportunity."
    if big_site_count >= 4:
        return "Low", "Many major sites in top results; difficult for new sites to enter."
    return "Medium", "Some opportunity; assess further with competitor pages."


def analyze_keyword(keyword, api_key):
    serp = fetch_serp(keyword, api_key)
    organic_results = as_list(
        find_first(serp, ("organic_results", "organic", "organicResults"))
    )
    paa = as_list(
        find_first(serp, ("people_also_ask", "related_questions", "peopleAlsoAsk"))
    )
    ads = as_list(
        find_first(
            serp,
            (
                "ads",
                "paid_results",
                "top_ads",
                "bottom_ads",
                "sponsored_results",
                "sponsored",
                "ad_results",
                "text_ads",
                "shopping_ads",
            ),
        )
    )
    ads_count = len(ads)
    if ads_count == 0:
        ads_count = (
            count_any(serp, ("top_ads",))
            + count_any(serp, ("bottom_ads",))
            + count_any(serp, ("sponsored_results",))
            + count_any(serp, ("sponsored",))
            + count_any(serp, ("ad_results",))
            + count_any(serp, ("text_ads",))
            + count_any(serp, ("shopping_ads",))
        )

    top_domains = []
    for item in organic_results[:10]:
        if not isinstance(item, dict):
            continue
        link = item.get("link") or item.get("url") or item.get("href") or ""
        domain = get_domain(link)
        if domain:
            top_domains.append(domain)

    opportunity_level, recommendation = classify_opportunity(
        len(paa), ads_count > 0, top_domains
    )

    analysis_row = {
        "keyword": keyword,
        "organic_count": len(organic_results),
        "paa_count": len(paa),
        "ads_count": ads_count,
        "ads_present": str(ads_count > 0).lower(),
        "top_domains": ", ".join(top_domains[:5]),
        "opportunity_level": opportunity_level,
        "recommendation": recommendation,
    }
    raw_record = {
        "keyword": keyword,
        "organic_results": organic_results,
        "paa": paa,
        "ads": ads,
        "top_domains": top_domains,
        "full_response": serp,
    }
    return analysis_row, raw_record


def main():
    api_key = TALORDATA_API_KEY.strip()
    if not api_key or api_key == "YOUR_TALORDATA_API_KEY_HERE":
        raise SystemExit(
            "Missing API key. Open analyze_serp_opportunities.py and set TALORDATA_API_KEY."
        )

    keyword_path = ROOT / "keywords.csv"
    output_path = ROOT / "serp_opportunities.csv"
    raw_output_path = ROOT / "serp_raw_records.jsonl"

    with keyword_path.open("r", encoding="utf-8-sig", newline="") as f:
        keywords = [row["keyword"].strip() for row in csv.DictReader(f) if row["keyword"].strip()]

    rows = []
    raw_records = []
    for keyword in keywords:
        print(f"Analyzing: {keyword}")
        row, raw_record = analyze_keyword(keyword, api_key)
        rows.append(row)
        raw_records.append(raw_record)

    fieldnames = [
        "keyword",
        "organic_count",
        "paa_count",
        "ads_count",
        "ads_present",
        "top_domains",
        "opportunity_level",
        "recommendation",
    ]
    try:
        handle = output_path.open("w", encoding="utf-8-sig", newline="")
    except PermissionError:
        stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
        output_path = ROOT / f"serp_opportunities_{stamp}.csv"
        handle = output_path.open("w", encoding="utf-8-sig", newline="")
        print(f"serp_opportunities.csv is locked. Writing to {output_path.name} instead.")

    with handle as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(rows)

    try:
        raw_handle = raw_output_path.open("w", encoding="utf-8")
    except PermissionError:
        stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
        raw_output_path = ROOT / f"serp_raw_records_{stamp}.jsonl"
        raw_handle = raw_output_path.open("w", encoding="utf-8")
        print(f"serp_raw_records.jsonl is locked. Writing to {raw_output_path.name} instead.")

    with raw_handle as f:
        for record in raw_records:
            f.write(json.dumps(record, ensure_ascii=False) + "\n")

    print(f"Done: {output_path}")
    print(f"Raw records: {raw_output_path}")


if __name__ == "__main__":
    main()

Enter fullscreen mode Exit fullscreen mode