Created
October 4, 2023 16:12
-
-
Save rkreddyp/b226cb3144bf8052d49b9984f98d345d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, os | |
# your paths, below is just an example on mac | |
sys.path .extend ( ['/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python311.zip', '/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11', '/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload'] ) | |
os.environ["SERPAPI_API_KEY"] = "xxx" | |
from serpapi import GoogleSearch | |
def serp_search (query_json) : | |
search = query_json['query'] | |
number = query_json['limit'] | |
params = { | |
"api_key": os.environ["SERPAPI_API_KEY"], # https://serpapi.com/manage-api-key | |
"engine": "google", # serpapi parsing engine | |
"q": search, # search query | |
"h1": "en", # language | |
"gl": "us", # country to search from | |
"google_domain": "google.com", | |
"num": number, | |
"tbs": "qdr:d3" # last week | |
} | |
search = GoogleSearch(params) # where data extraction happens | |
results = search.get_dict() # returns an iterator of all pages | |
news_dict_arr = [] | |
for result in results["organic_results"]: | |
news_dict_arr.append(result) | |
#titles_and_links = [ {'title': news_dict_arr[i]['title'], 'link': news_dict_arr[i]['link'], 'snippet': news_dict_arr[i]['snippet']} for i in range(len(news_dict_arr)) ] | |
titles_and_links = [ {'title': news_dict_arr[i]['title'], 'link': news_dict_arr[i]['link'] } for i in range(len(news_dict_arr)) ] | |
print (titles_and_links) | |
return titles_and_links | |
serp_search({'query': 'is oil a good investment', 'limit': 10}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment