|
| 1 | +import re |
| 2 | +from enum import Enum |
| 3 | +from typing import Tuple, Optional |
| 4 | + |
| 5 | +import requests |
| 6 | +from bs4 import BeautifulSoup |
| 7 | +from pydantic import BaseModel |
| 8 | + |
| 9 | + |
| 10 | +import time |
| 11 | +from selenium.webdriver.common.by import By |
| 12 | +from selenium.webdriver.support.ui import WebDriverWait |
| 13 | +from selenium.webdriver.support import expected_conditions as EC |
| 14 | +from selenium.webdriver.common.action_chains import ActionChains |
| 15 | +from selenium.webdriver.common.keys import Keys |
| 16 | + |
| 17 | +def scroll_down(driver): |
| 18 | + """Scroll down the webpage using ActionChains.""" |
| 19 | + actions = ActionChains(driver) |
| 20 | + body = driver.find_element(By.TAG_NAME, 'body') |
| 21 | + body.click() |
| 22 | + actions.send_keys(Keys.PAGE_DOWN).perform() |
| 23 | + actions.send_keys(Keys.PAGE_DOWN).perform() |
| 24 | + |
| 25 | + time.sleep(0.5) |
| 26 | + actions.send_keys(Keys.PAGE_DOWN).perform() |
| 27 | + actions.send_keys(Keys.PAGE_DOWN).perform() |
| 28 | + time.sleep(0.5) |
| 29 | + |
| 30 | + |
| 31 | +def get_piotroski_f_score(driver, url): |
| 32 | + driver.get(url) |
| 33 | + try: |
| 34 | + # Wait for the table containing the Piotroski F-Score to load |
| 35 | + scroll_down(driver) |
| 36 | + |
| 37 | + WebDriverWait(driver, 15).until( |
| 38 | + EC.presence_of_element_located((By.CSS_SELECTOR, "div.relative-box.children-card")) |
| 39 | + ) |
| 40 | + |
| 41 | + last_breadcrumb = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, "//div[@aria-label='Breadcrumb']//span[@class='el-breadcrumb__item'][last()]//span[@class='el-breadcrumb__inner']"))) |
| 42 | + name = last_breadcrumb.text.strip() |
| 43 | + # Locate the table row containing the Piotroski F-Score and get the corresponding score |
| 44 | + score_element = driver.find_element(By.XPATH, "//div[@class='relative-box children-card']//table//tr[1]//td[@class='t-right t-primary']") |
| 45 | + f_score = score_element.text.strip() |
| 46 | + |
| 47 | + # Wait for the GF Score element and retrieve it |
| 48 | + gfscore_element = WebDriverWait(driver, 15).until( |
| 49 | + EC.presence_of_element_located((By.XPATH, "//div[contains(@id, 'gf-score-section-')]//span[@class='t-primary']")) |
| 50 | + ) |
| 51 | + gf_score = gfscore_element.text.strip() |
| 52 | + |
| 53 | + return name, f_score, gf_score |
| 54 | + |
| 55 | + except Exception as e: |
| 56 | + print(f"Error: {e}") |
| 57 | + return None |
| 58 | + |
| 59 | +# # Initialize the WebDriver |
| 60 | +# driver = webdriver.Chrome() |
| 61 | + |
| 62 | +# # URL to scrape |
| 63 | +# url = "https://www.gurufocus.com/stock/CHIX:VITBS/summary" |
| 64 | + |
| 65 | +# # Get the Piotroski F-Score |
| 66 | +# f_score, g_score = get_piotroski_f_score(driver, url) |
| 67 | +# if f_score: |
| 68 | +# print(f"Piotroski F-score: {f_score} found. GF score: {g_score}") |
0 commit comments