finviz-data 1.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
finviz_data/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
|
|
3
|
+
from bs4 import BeautifulSoup
|
|
4
|
+
from curl_cffi import requests
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def get_soup(ticker) -> BeautifulSoup:
|
|
8
|
+
url = f"https://finviz.com/stock?t={ticker}&p=d"
|
|
9
|
+
html = requests.get(url, impersonate="chrome").text
|
|
10
|
+
soup = BeautifulSoup(html, "html.parser")
|
|
11
|
+
return soup
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def get_fundamentals(soup: BeautifulSoup) -> dict:
|
|
15
|
+
# get table with class='snapshot-table2'
|
|
16
|
+
table = soup.find("table", {"class": "snapshot-table2"})
|
|
17
|
+
|
|
18
|
+
# Initialize a dictionary to store the key-value pairs
|
|
19
|
+
financial_data = {}
|
|
20
|
+
|
|
21
|
+
# Iterate through all rows of the table
|
|
22
|
+
for row in table.find_all("tr"):
|
|
23
|
+
# Each cell in the row
|
|
24
|
+
cells = row.find_all("td")
|
|
25
|
+
for i in range(0, len(cells), 2): # Step by 2 as key and value are in pairs
|
|
26
|
+
key = cells[i].get_text().strip()
|
|
27
|
+
value = cells[i + 1].get_text().strip()
|
|
28
|
+
financial_data[key] = value
|
|
29
|
+
|
|
30
|
+
return financial_data
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def get_fundamentals_float(soup: BeautifulSoup) -> dict:
|
|
34
|
+
fundamentals = get_fundamentals(soup)
|
|
35
|
+
fundamentals_float = _convert_to_floats(fundamentals)
|
|
36
|
+
|
|
37
|
+
return fundamentals_float
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def get_company_info(soup: BeautifulSoup) -> dict:
|
|
41
|
+
base_info = {}
|
|
42
|
+
|
|
43
|
+
# Get ticker. Text ontent of quote-header_ticker-wrapper_ticker
|
|
44
|
+
ticker = soup.find("h1", class_="quote-header_ticker-wrapper_ticker").text
|
|
45
|
+
base_info["Ticker"] = ticker
|
|
46
|
+
|
|
47
|
+
# Get company name. Text content of h2.quote-header_ticker-wrapper_company a
|
|
48
|
+
company_name = (
|
|
49
|
+
soup.find("h2", class_="quote-header_ticker-wrapper_company").text
|
|
50
|
+
)
|
|
51
|
+
base_info["Company"] = company_name.strip()
|
|
52
|
+
|
|
53
|
+
# Finding the first inner div of the element with class 'quote-links'
|
|
54
|
+
first_inner_div = soup.find("div", class_="quote-links").find("div")
|
|
55
|
+
|
|
56
|
+
# Extracting all links from the first inner div
|
|
57
|
+
links = first_inner_div.find_all("a")
|
|
58
|
+
|
|
59
|
+
for link in links:
|
|
60
|
+
href = link.get("href", "")
|
|
61
|
+
if "f=sec_" in href:
|
|
62
|
+
base_info["Sector"] = link.text
|
|
63
|
+
elif "f=ind_" in href:
|
|
64
|
+
base_info["Industry"] = link.text
|
|
65
|
+
elif "f=geo_" in href:
|
|
66
|
+
base_info["Country"] = link.text
|
|
67
|
+
elif "f=exch_" in href:
|
|
68
|
+
base_info["Exchange"] = link.text
|
|
69
|
+
|
|
70
|
+
return base_info
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _is_float_like(val: str) -> bool:
|
|
74
|
+
try:
|
|
75
|
+
float(val)
|
|
76
|
+
return True
|
|
77
|
+
except ValueError:
|
|
78
|
+
return False
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _convert_value(value: str):
|
|
82
|
+
if value is None:
|
|
83
|
+
return None
|
|
84
|
+
|
|
85
|
+
value = value.strip()
|
|
86
|
+
|
|
87
|
+
if value in ("", "-"):
|
|
88
|
+
return None
|
|
89
|
+
elif value.endswith("%") and _is_float_like(value.strip("%")):
|
|
90
|
+
return float(value.strip("%")) / 100
|
|
91
|
+
elif value.endswith("M") and _is_float_like(value.strip("M")):
|
|
92
|
+
return float(value.strip("M")) * 1e6
|
|
93
|
+
elif value.endswith("B") and _is_float_like(value.strip("B")):
|
|
94
|
+
return float(value.strip("B")) * 1e9
|
|
95
|
+
elif value.endswith("T") and _is_float_like(value.strip("T")):
|
|
96
|
+
return float(value.strip("T")) * 1e12
|
|
97
|
+
elif _is_float_like(value.replace(",", "")):
|
|
98
|
+
return float(value.replace(",", ""))
|
|
99
|
+
else:
|
|
100
|
+
return value
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _convert_date(value: str):
|
|
104
|
+
try:
|
|
105
|
+
return datetime.strptime(value, "%b %d, %Y").date().isoformat()
|
|
106
|
+
except ValueError:
|
|
107
|
+
return _convert_value(value)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _split_two_values(value: str, first_key: str, second_key: str) -> dict:
|
|
111
|
+
values = value.split()
|
|
112
|
+
if len(values) != 2:
|
|
113
|
+
return {first_key: _convert_value(value), second_key: None}
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
first_key: _convert_value(values[0]),
|
|
117
|
+
second_key: _convert_value(values[1]),
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _split_dividend(value: str, amount_key: str, yield_key: str) -> dict:
|
|
122
|
+
values = value.replace("(", "").replace(")", "").split()
|
|
123
|
+
if len(values) != 2:
|
|
124
|
+
return {amount_key: _convert_value(value), yield_key: None}
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
amount_key: _convert_value(values[0]),
|
|
128
|
+
yield_key: _convert_value(values[1]),
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _split_option_short(value: str) -> dict:
|
|
133
|
+
values = [item.strip() for item in value.split("/")]
|
|
134
|
+
if len(values) != 2:
|
|
135
|
+
return {"Optionable": _convert_value(value), "Shortable": None}
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
"Optionable": values[0] == "Yes",
|
|
139
|
+
"Shortable": values[1] == "Yes",
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _split_earnings(value: str) -> dict:
|
|
144
|
+
values = value.split()
|
|
145
|
+
if len(values) < 3:
|
|
146
|
+
return {"Earnings Date": _convert_value(value), "Earnings Time": None}
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
"Earnings Date": " ".join(values[:-1]),
|
|
150
|
+
"Earnings Time": values[-1],
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _convert_to_floats(data_dict: dict) -> dict:
|
|
155
|
+
"""
|
|
156
|
+
Converts values in a dictionary to floats where applicable.
|
|
157
|
+
- Replaces single dash ('-') with None.
|
|
158
|
+
- Splits values representing a range (e.g., '10.86 - 19.08') into two separate keys.
|
|
159
|
+
- Converts values with '%' to proportionate floats.
|
|
160
|
+
- Converts values ending in 'M', 'B', or 'T' to their numeric equivalents in millions, billions, or trillions.
|
|
161
|
+
- Converts numeric strings with commas (e.g., '30,196,932') into floats.
|
|
162
|
+
"""
|
|
163
|
+
|
|
164
|
+
compound_fields = {
|
|
165
|
+
"Volatility": lambda value: _split_two_values(
|
|
166
|
+
value, "Volatility Week", "Volatility Month"
|
|
167
|
+
),
|
|
168
|
+
"52W High": lambda value: _split_two_values(
|
|
169
|
+
value, "52W High Price", "52W High Change"
|
|
170
|
+
),
|
|
171
|
+
"52W Low": lambda value: _split_two_values(
|
|
172
|
+
value, "52W Low Price", "52W Low Change"
|
|
173
|
+
),
|
|
174
|
+
"EPS past 3/5Y": lambda value: _split_two_values(
|
|
175
|
+
value, "EPS past 3Y", "EPS past 5Y"
|
|
176
|
+
),
|
|
177
|
+
"Sales past 3/5Y": lambda value: _split_two_values(
|
|
178
|
+
value, "Sales past 3Y", "Sales past 5Y"
|
|
179
|
+
),
|
|
180
|
+
"Dividend Gr. 3/5Y": lambda value: _split_two_values(
|
|
181
|
+
value, "Dividend Gr. 3Y", "Dividend Gr. 5Y"
|
|
182
|
+
),
|
|
183
|
+
"EPS/Sales Surpr.": lambda value: _split_two_values(
|
|
184
|
+
value, "EPS Surprise", "Sales Surprise"
|
|
185
|
+
),
|
|
186
|
+
"Dividend Est.": lambda value: _split_dividend(
|
|
187
|
+
value, "Dividend Est. Amount", "Dividend Est. Yield"
|
|
188
|
+
),
|
|
189
|
+
"Dividend TTM": lambda value: _split_dividend(
|
|
190
|
+
value, "Dividend TTM Amount", "Dividend TTM Yield"
|
|
191
|
+
),
|
|
192
|
+
"Option/Short": _split_option_short,
|
|
193
|
+
"Earnings": _split_earnings,
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
date_fields = {"IPO", "Dividend Ex-Date"}
|
|
197
|
+
|
|
198
|
+
new_data = {}
|
|
199
|
+
for key, value in data_dict.items():
|
|
200
|
+
value = value.strip() if isinstance(value, str) else value
|
|
201
|
+
|
|
202
|
+
if key in compound_fields:
|
|
203
|
+
new_data.update(compound_fields[key](value))
|
|
204
|
+
elif key in date_fields:
|
|
205
|
+
new_data[key] = _convert_date(value)
|
|
206
|
+
else:
|
|
207
|
+
new_data[key] = _convert_value(value)
|
|
208
|
+
|
|
209
|
+
return new_data
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: finviz-data
|
|
3
|
+
Version: 1.2.2
|
|
4
|
+
Summary: Simple package to get data from finviz.com
|
|
5
|
+
Author-email: Dennis Iversen <dennis.iversen@gmail.com>
|
|
6
|
+
Project-URL: Repository, https://github.com/diversen/finviz-data
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
Requires-Dist: beautifulsoup4<5,>=4.12
|
|
10
|
+
Requires-Dist: curl-cffi<1,>=0.7
|
|
11
|
+
|
|
12
|
+
# finviz-data
|
|
13
|
+
|
|
14
|
+
A simple package for getting fundamental data from finviz.com for a single ticker.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
uv add git+https://github.com/diversen/finviz-data
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Development
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
uv sync
|
|
26
|
+
uv run python -m unittest discover
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
|
|
33
|
+
from finviz_data import finviz_data
|
|
34
|
+
|
|
35
|
+
# Get the html soup for a single ticker
|
|
36
|
+
soup = finviz_data.get_soup('AAPL')
|
|
37
|
+
|
|
38
|
+
# Get the fundamentals for a single ticker
|
|
39
|
+
fundamentals = finviz_data.get_fundamentals(soup)
|
|
40
|
+
|
|
41
|
+
# Get the fundamentals where all is formatted to float values where possible
|
|
42
|
+
fundamentals = finviz_data.get_fundementals_float(soup)
|
|
43
|
+
|
|
44
|
+
# Get basic company info, sector, ticker etc.
|
|
45
|
+
company_info = finviz_data.get_company_info(soup)
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
finviz_data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
finviz_data/finviz_data.py,sha256=yXaqvlAs8C2ag9f5ZFxpl1gqvu6cyJDVmGafg2s5iOA,6687
|
|
3
|
+
finviz_data-1.2.2.dist-info/METADATA,sha256=InA1KPhaZlDxjqIzEhhIBn_DunKiJHXp-xjPQUEpj_k,1076
|
|
4
|
+
finviz_data-1.2.2.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
5
|
+
finviz_data-1.2.2.dist-info/top_level.txt,sha256=ThOlyRUZfe1iYFLpNodzkEuRUK8NvvAw-EqavoNR_m8,12
|
|
6
|
+
finviz_data-1.2.2.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
finviz_data
|