Add websearch module

This commit is contained in:
Cohee
2023-11-28 20:36:02 +02:00
parent 085b0a849d
commit 05cab1c918
4 changed files with 75 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.firefox.service import Service as FirefoxService
def get_driver():
try:
print("Initializing Chrome driver...")
chromeService = ChromeService()
options = ChromeOptions()
options.add_argument('--disable-infobars')
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--lang=en-GB")
return webdriver.Chrome(service=chromeService, options=options)
except:
print("Chrome not found, using Firefox instead.")
firefoxService = FirefoxService()
options = FirefoxOptions()
options.add_argument("--headless")
options.set_preference("intl.accept_languages", "en,en_US")
return webdriver.Firefox(service=firefoxService, options=options)
def search_google(query: str) -> str:
driver = get_driver()
print("Searching for " + query + "...")
driver.get("https://google.com/search?hl=en&q=" + query)
text = ''
# Answer box
for el in driver.find_elements(By.CSS_SELECTOR, '.hgKElc'):
if el and el.text:
text += el.text + '\n'
# Knowledge panel
for el in driver.find_elements(By.CSS_SELECTOR, '.hgKElc'):
if el and el.text:
text += el.text + '\n'
# Page snippets
for el in driver.find_elements(By.CSS_SELECTOR, '.yDYNvb.lyLwlc'):
if el and el.text:
text += el.text + '\n'
print("Found: " + text)
driver.quit()
return text