جهزوا بيئتكم
سوف تستخدم Python 3 و مكتبته العادية خلق مفتاح API وتأكد من أن حسابك متاح إذا كان هذا هو طلبك الأول، انظر API faststart.
# Replace YOUR_API_KEY near the top of the code below.النص الوارد أدناه يبحث عن openai باستخدام نتائج الولايات المتحدة وواجهة إنجليزية، ثم يكتب الوصلات العضوية المعادة إلى ملف CSV.
إجلبْ وإنقاذ نتائجَ البحثِ
احفظ النص كما search.py. وهي تقدم طلباً في الوقت الحقيقي مع source: "google_search" ويقرأ organic صفيفة من النتيجة الأولى
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")
python3 search.pyافتح الباب google-results.csv في صحيفة أو تحميلها في طلبك الأعمدة المصدرة هي الموقع، العنوان، URL والقنبلة ويتعامل كاتب شركة Python في شركة CSV مع اقتباسات السفريات وكسر الخط في نص النتائج.
فهم رد API
ويبين هذا الرد المسجل أن الهيكل قد عاد من أجل البحث عن نظام " أوف آي " . وتصدر النصوص وصلات عضوية؛ وتظل الأقسام الأخرى المتاحة في استجابة API إذا كان تطبيقك يحتاج إليها.
{
"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"
}The num يطلب عددًا من النتائج. ولا يضمن أن تعيد Google هذا العدد من الروابط العضوية. يحفظ السكربت النتائج التي أُعيدت بالفعل.
غيّر البحث
التغيير query من أجل موضوعك, params.gl من أجل البلد params.hl للغة الواجهة الاستخدام params.page (ب) أن تطلب صفحة أخرى؛ وكل صفحة هي مكالمة منفصلة من API.
لمقارنة النتائج مع مرور الوقت، تخزين الاستفسارات، المنطقة، وقت التحصيل إلى جانب كل صف. The API يُعيد طلقات بحثية، المقارنات التاريخية تأتي من الطلقات التي تُنقذها.