-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (42 loc) · 1.48 KB
/
Copy pathmain.py
File metadata and controls
53 lines (42 loc) · 1.48 KB
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
53
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from halo import Halo
# Initialize vars
query = input("Input your query: ")
url = "https://www.phind.com/search?q=" + query
# suppress logging
chrome_options = Options()
chrome_options.add_argument("--log-level=3")
# make the browser not open
chrome_options.add_argument("--headless")
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
# init the webdriver
driver = webdriver.Chrome(options=chrome_options)
try:
# Start the spinner
spinner = Halo("Getting Answer from Phind...\n\n\n\n\n \n", spinner="dots")
spinner.start()
# Get the page content
driver.get(url)
# wait until its all loaded
WebDriverWait(driver, timeout=50).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
time.sleep(15)
# Get the elements
answer_elements = driver.find_elements(By.CSS_SELECTOR, "main div.fs-5")
# init the list
paragraph_texts = []
# get content
for answer_element in answer_elements:
paragraph_texts.append(answer_element.text.strip())
# print it
for text in paragraph_texts:
# Stop spinner
spinner.stop()
print(text)
finally:
# Close the browser
driver.quit()