Skip to content

Commit 0501453

Browse files
committed
Add gurufocus
1 parent 38d6789 commit 0501453

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

Trading/stock/gurufocus/cli.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
3+
from Trading.stock.gurufocus.gurufocus import get_piotroski_f_score
4+
from Trading.utils.google_search import get_first_google_result
5+
from Trading.utils.cli import Named
6+
from Trading.utils.custom_logging import get_logger
7+
import fire
8+
from selenium import webdriver
9+
import time
10+
11+
LOGGER = get_logger(__file__)
12+
13+
14+
# DEBUG=true cli.py analyze --names '["pdco", "paypal", "johnson&johnson", "mcdonalds", "pepsi", "uniper", "palantir"]'
15+
class GuruFocusCLI(Named):
16+
def analyze(self):
17+
if not self.name and not self.names:
18+
LOGGER.error("Name is required")
19+
urls = []
20+
if self.name:
21+
urls.append(get_first_google_result("gurufocus summary " + self.name))
22+
if self.names:
23+
for name in self.names:
24+
LOGGER.debug(f"Searching for: {name}")
25+
urls.append(get_first_google_result("gurufocus summary " + name))
26+
27+
LOGGER.debug(f"URLs: {urls}")
28+
29+
driver = webdriver.Chrome()
30+
for url in urls:
31+
LOGGER.info(f"Scraping {url}")
32+
name, f_score, g_score = get_piotroski_f_score(driver, url)
33+
LOGGER.info(f"{name} " f"Piotroski F-score: {f_score}. GF score: {g_score}")
34+
35+
time.sleep(0.5)
36+
driver.quit()
37+
38+
if __name__ == "__main__":
39+
fire.Fire(GuruFocusCLI)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Comments
 (0)