ข้ามไปยังเนื้อหา
TH
รับคีย์ API

Google Search / Python

รับ Google ผลการค้นหาด้วย Python และช่วยพวกมัน CSV

ส่ง Google Search API ร้องขอ, อ่านผลของออแกนิก และชื่อการส่งออก URLs และดอกไม้ที่แตกกระจาย Pythonห้อง สมุด มาตรฐาน.

FlashData · จัดพิมพ์

ค้นหาไพ่ผลลัพธ์ที่เรียงเป็นตารางคํานวณ

เตรียมสภาพแวดล้อมของคุณ

คุณจะใช้ Python 3 และห้องสมุดมาตรฐาน สร้างรายการ API กุญแจ และตรวจสอบให้แน่ใจว่า บัญชีของคุณมีเครดิตที่มี ถ้านี่เป็นคําขอร้องครั้งแรกของคุณ ดู API เริ่มทํางานเร็ว.

bash
# Replace YOUR_API_KEY near the top of the code below.

สคริปต์ด้านล่างนี้สําหรับค้นหา openai โดยใช้ผลลัพธ์ US และอินเทอร์เฟซภาษาอังกฤษ แล้วเขียนลิงก์อินทรีย์ที่ส่งกลับ CSV แฟ้ม

ดึงข้อมูลและบันทึกผลลัพธ์การค้นหา

บันทึกสคริปต์เป็น search.py. มันส่งคําขอแบบเรียลไทม์ source: "google_search" และอ่าน organic จากผลแรก

python
import csv
import json
import os
import urllib.request

base_url = "https://data.flashdata.dev"
api_key = "YOUR_API_KEY"
request = urllib.request.Request(
    base_url + "/v1/queries/realtime",
    data=json.dumps({
        "source": "google_search",
        "query": "openai",
        "params": {"gl": "us", "hl": "en", "num": 10},
    }).encode("utf-8"),
    headers={
        "X-API-Key": api_key,
        "Content-Type": "application/json",
    },
    method="POST",
)

with urllib.request.urlopen(request, timeout=60) as response:
    payload = json.load(response)

if payload.get("status") != "done":
    raise RuntimeError(payload.get("message", "Search did not complete"))

results = payload["results"][0].get("organic", [])
with open("google-results.csv", "w", newline="", encoding="utf-8") as output:
    writer = csv.DictWriter(output, fieldnames=["position", "title", "url", "snippet"])
    writer.writeheader()
    for result in results:
        writer.writerow({
            "position": result.get("position", ""),
            "title": result.get("title", ""),
            "url": result.get("link", ""),
            "snippet": result.get("snippet", ""),
        })

print(f"Saved {len(results)} results to google-results.csv")
bash
python3 search.py

เปิด google-results.csv ในตารางคํานวณ หรือโหลดเข้าไปในโปรแกรมของคุณ คอลัมน์ที่ส่งออกคือตําแหน่ง, ชื่อ URL และเนื้อร้าย Pythonของ CSV ผู้เขียนจัดการการอ้างถึงเครื่องหมายจุลภาคและบรรทัดแบ่งภายในข้อความผลลัพธ์

เข้าใจ API ตอบกลับ

การตอบสนองนี้บันทึกแสดงโครงสร้างที่กลับมาสําหรับการค้นหา OpenAI การส่งออกสคริปต์ จะเป็นการส่งลิงก์แบบออแกนิก ส่วนอื่น ๆ ที่ใช้ได้ยังคงอยู่ใน API ตอบมาถ้าใบสมัครของคุณต้องการมัน

json
{
  "source": "google_search",
  "status": "done",
  "results": [
    {
      "searchParameters": {
        "q": "openai",
        "gl": "us",
        "hl": "en",
        "type": "search",
        "num": 10,
        "page": 1,
        "engine": "google"
      },
      "organic": [
        {
          "title": "OpenAI | Research & Deployment",
          "link": "https://openai.com/",
          "snippet": "We believe our research will eventually lead to artificial general intelligence, a system that can solve human-level problems.",
          "position": 1
        },
        {
          "title": "OpenAI",
          "link": "https://en.wikipedia.org/wiki/OpenAI",
          "snippet": "OpenAI is an American artificial intelligence (AI) public benefit corporation (PBC) headquartered in San Francisco. It develops proprietary generative AI models ...",
          "position": 2
        },
        {
          "title": "OpenAI",
          "link": "https://x.com/OpenAI",
          "snippet": "OpenAI's mission is to ensure that artificial general intelligence benefits all of humanity. can develop research, build financial models, and create ...",
          "position": 3
        },
        {
          "title": "OpenAI",
          "link": "https://www.linkedin.com/company/openai",
          "snippet": "OpenAI is an AI research and deployment company dedicated to ensuring that general-purpose artificial intelligence benefits all of humanity.",
          "position": 4
        },
        {
          "title": "r/OpenAI",
          "link": "https://www.reddit.com/r/OpenAI/",
          "snippet": "OpenAI is an AI research and deployment company. OpenAI's mission is to create safe and powerful AI that benefits all of humanity. OpenAI's ...",
          "position": 5
        }
      ],
      "peopleAlsoAsk": [
        {
          "question": "Is OpenAI created by Elon Musk?"
        },
        {
          "question": "Why are people leaving ChatGPT?"
        },
        {
          "question": "Is OpenAI the same as ChatGPT?"
        },
        {
          "question": "Why is OpenAI in trouble?"
        }
      ],
      "relatedSearches": [
        {
          "query": "OpenAI login"
        },
        {
          "query": "OpenAI chat"
        },
        {
          "query": "OpenAI ChatGPT"
        },
        {
          "query": "OpenAI - wikipedia"
        },
        {
          "query": "OpenAI API"
        },
        {
          "query": "OpenAI app"
        },
        {
          "query": "OpenAI download"
        },
        {
          "query": "OpenAI video"
        }
      ]
    }
  ],
  "usage": {
    "credits": 0.02
  },
  "created_at": "2026-09-16T07:41:40.043Z"
}

เดอะ num พารามิเตอร์ร้องขอจํานวนผลลัพธ์ มันไม่ได้รับประกันว่า Google จะส่งลิงก์ออแกนิกจํานวนมากกลับมา สคริปต์จะบันทึกผลลัพธ์ต่าง ๆ ไว้

เปลี่ยนการค้นหา

เปลี่ยน query สําหรับหัวข้อของคุณ, params.gl สําหรับประเทศและ params.hl สําหรับภาษาอินเตอร์เฟส ใช้ params.page เพื่อร้องขออีกหน้าหนึ่ง แต่ละหน้าแยกต่างหาก API โทรสิ

เพื่อ เปรียบ เทียบ ผล ที่ จะ ตาม มา ให้ เก็บ ข้อ ไต่ถาม, ภูมิภาค, และ เวลา รวบ รวม ไว้ ข้าง ๆ แถว. เดอะ API ตอบกลับการจับภาพการค้นหา; การเปรียบเทียบทางประวัติศาสตร์มาจากภาพที่คุณบันทึก