-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_socials.py
52 lines (43 loc) · 1.69 KB
/
search_socials.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# google search engine details (cx is the code)
# <script async src="https://cse.google.com/cse.js?cx=73d7fdc99fe7141b9">
# </script>
# <div class="gcse-search"></div>
# api key: AIzaSyBl9WeKsCwckDpgbQ9DoPgYz5fQ3ITXksk
import json
import requests
API_KEY = 'AIzaSyBl9WeKsCwckDpgbQ9DoPgYz5fQ3ITXksk'
CX = '73d7fdc99fe7141b9'
def google_search(query):
search_url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={API_KEY}&cx={CX}"
response = requests.get(search_url)
if response.status_code == 200:
search_results = response.json()
return search_results
else:
print(f"Error: HTTP {response.status_code}")
try:
print("Response:", response.json())
except json.JSONDecodeError:
print("Failed to parse response as JSON")
return None
location = str(input("school name: ")) # ex. Hope Technology School
domain = str(input("social media website name (ex. facebook, instagram): ")) # ex. Facebook
query = location + " " + domain
results = google_search(query)
if results:
# Extract the search results
search_items = results.get("items", [])
if not search_items:
print("No search results found.")
else:
print("Results found for " + query)
search_results = [{"title": item["title"], "link": item["link"]} for item in search_items]
# Save to JSON
file_path = "google_search_results.json"
with open(file_path, "w") as file:
json.dump(search_results, file, indent=4)
print("Search results saved to google_search_results.json")
with open(file_path, "r") as file:
print(file.read())
else:
print("Failed to retrieve search results.")