अपना वातावरण तैयार करें
आप Python 3 और इसके मानक पुस्तकालय का उपयोग करेंगे। बनाना API कुंजी और सुनिश्चित करें कि आपका खाता क्रेडिट उपलब्ध है। यदि यह आपका पहला अनुरोध है, तो देखें API क्विकस्टार्ट.
# 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 प्रतिक्रिया को समझें
यह दर्ज की गई प्रतिक्रिया एक OpenAI खोज के लिए वापस आ गई संरचना को दर्शाती है। स्क्रिप्ट जैविक लिंक निर्यात करता है; अन्य उपलब्ध अनुभाग 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"
}यह num मापदंड एक परिणाम गिनती का अनुरोध करता है। यह गारंटी नहीं देता कि Google उस समय कई कार्बनिक लिंक वापस आ जाएंगे। हालांकि कई परिणामों को बचाता है।
खोज बदलें
परिवर्तन query अपने विषय के लिए, params.gl देश और देश के लिए params.hl इंटरफ़ेस भाषा के लिए। उपयोग params.page एक दूसरे पृष्ठ का अनुरोध करने के लिए; प्रत्येक पृष्ठ एक अलग API कॉल है।
समय के साथ परिणामों की तुलना करने के लिए, प्रत्येक पंक्ति के साथ क्वेरी, क्षेत्र और संग्रह समय को स्टोर करें। API एक खोज स्नैपशॉट लौटाता है; ऐतिहासिक तुलना उन स्नैपशॉट्स से आती है जिन्हें आप बचाते हैं।