finviz-data 1.3.1__tar.gz
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-1.3.1/PKG-INFO +56 -0
- finviz_data-1.3.1/README.md +45 -0
- finviz_data-1.3.1/finviz_data/__init__.py +0 -0
- finviz_data-1.3.1/finviz_data/finviz_data.py +223 -0
- finviz_data-1.3.1/finviz_data.egg-info/PKG-INFO +56 -0
- finviz_data-1.3.1/finviz_data.egg-info/SOURCES.txt +10 -0
- finviz_data-1.3.1/finviz_data.egg-info/dependency_links.txt +1 -0
- finviz_data-1.3.1/finviz_data.egg-info/requires.txt +2 -0
- finviz_data-1.3.1/finviz_data.egg-info/top_level.txt +1 -0
- finviz_data-1.3.1/pyproject.toml +23 -0
- finviz_data-1.3.1/setup.cfg +4 -0
- finviz_data-1.3.1/tests/test_finviz_data.py +175 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: finviz-data
|
|
3
|
+
Version: 1.3.1
|
|
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
|
+
By default, the live Finviz test is skipped. To run the full test suite without
|
|
30
|
+
skipping the live test, enable it with `FINVIZ_LIVE_TESTS=1`:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
FINVIZ_LIVE_TESTS=1 uv run python -m unittest discover
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The live test makes a real request to finviz.com and may still be skipped if
|
|
37
|
+
Finviz temporarily rate limits your IP.
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
|
|
43
|
+
from finviz_data import finviz_data
|
|
44
|
+
|
|
45
|
+
# Get the html soup for a single ticker
|
|
46
|
+
soup = finviz_data.get_soup('AAPL')
|
|
47
|
+
|
|
48
|
+
# Get the fundamentals for a single ticker
|
|
49
|
+
fundamentals = finviz_data.get_fundamentals(soup)
|
|
50
|
+
|
|
51
|
+
# Get the fundamentals where all is formatted to float values where possible
|
|
52
|
+
fundamentals = finviz_data.get_fundementals_float(soup)
|
|
53
|
+
|
|
54
|
+
# Get basic company info, sector, ticker etc.
|
|
55
|
+
company_info = finviz_data.get_company_info(soup)
|
|
56
|
+
```
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# finviz-data
|
|
2
|
+
|
|
3
|
+
A simple package for getting fundamental data from finviz.com for a single ticker.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
uv add git+https://github.com/diversen/finviz-data
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Development
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
uv sync
|
|
15
|
+
uv run python -m unittest discover
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
By default, the live Finviz test is skipped. To run the full test suite without
|
|
19
|
+
skipping the live test, enable it with `FINVIZ_LIVE_TESTS=1`:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
FINVIZ_LIVE_TESTS=1 uv run python -m unittest discover
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The live test makes a real request to finviz.com and may still be skipped if
|
|
26
|
+
Finviz temporarily rate limits your IP.
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
|
|
32
|
+
from finviz_data import finviz_data
|
|
33
|
+
|
|
34
|
+
# Get the html soup for a single ticker
|
|
35
|
+
soup = finviz_data.get_soup('AAPL')
|
|
36
|
+
|
|
37
|
+
# Get the fundamentals for a single ticker
|
|
38
|
+
fundamentals = finviz_data.get_fundamentals(soup)
|
|
39
|
+
|
|
40
|
+
# Get the fundamentals where all is formatted to float values where possible
|
|
41
|
+
fundamentals = finviz_data.get_fundementals_float(soup)
|
|
42
|
+
|
|
43
|
+
# Get basic company info, sector, ticker etc.
|
|
44
|
+
company_info = finviz_data.get_company_info(soup)
|
|
45
|
+
```
|
|
File without changes
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
|
|
3
|
+
from bs4 import BeautifulSoup
|
|
4
|
+
from curl_cffi import requests
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class FinvizRequestError(Exception):
|
|
8
|
+
"""Raised when Finviz returns an unsuccessful HTTP response."""
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def get_soup(ticker) -> BeautifulSoup:
|
|
12
|
+
url = f"https://finviz.com/stock?t={ticker}&p=d"
|
|
13
|
+
response = requests.get(url, impersonate="chrome")
|
|
14
|
+
status_code = getattr(response, "status_code", None)
|
|
15
|
+
if isinstance(status_code, int) and status_code >= 400:
|
|
16
|
+
raise FinvizRequestError(
|
|
17
|
+
f"Finviz request failed for ticker {ticker!r}: "
|
|
18
|
+
f"HTTP {status_code} ({url})"
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
html = response.text
|
|
22
|
+
soup = BeautifulSoup(html, "html.parser")
|
|
23
|
+
return soup
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def get_fundamentals(soup: BeautifulSoup) -> dict:
|
|
27
|
+
# Finviz may render the snapshot as one table or split it into several
|
|
28
|
+
# responsive columns, all with the same class.
|
|
29
|
+
tables = soup.find_all("table", {"class": "snapshot-table2"})
|
|
30
|
+
|
|
31
|
+
# Initialize a dictionary to store the key-value pairs
|
|
32
|
+
financial_data = {}
|
|
33
|
+
|
|
34
|
+
for table in tables:
|
|
35
|
+
# Iterate through all rows of the table
|
|
36
|
+
for row in table.find_all("tr"):
|
|
37
|
+
# Each cell in the row
|
|
38
|
+
cells = row.find_all("td")
|
|
39
|
+
# Step by two because the cells are key/value pairs.
|
|
40
|
+
for i in range(0, len(cells), 2):
|
|
41
|
+
key = cells[i].get_text().strip()
|
|
42
|
+
value = cells[i + 1].get_text().strip()
|
|
43
|
+
financial_data[key] = value
|
|
44
|
+
|
|
45
|
+
return financial_data
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def get_fundamentals_float(soup: BeautifulSoup) -> dict:
|
|
49
|
+
fundamentals = get_fundamentals(soup)
|
|
50
|
+
fundamentals_float = _convert_to_floats(fundamentals)
|
|
51
|
+
|
|
52
|
+
return fundamentals_float
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def get_company_info(soup: BeautifulSoup) -> dict:
|
|
56
|
+
base_info = {}
|
|
57
|
+
|
|
58
|
+
# Get ticker. Text ontent of quote-header_ticker-wrapper_ticker
|
|
59
|
+
ticker = soup.find("h1", class_="quote-header_ticker-wrapper_ticker").text
|
|
60
|
+
base_info["Ticker"] = ticker
|
|
61
|
+
|
|
62
|
+
# Get company name. Text content of h2.quote-header_ticker-wrapper_company a
|
|
63
|
+
company_name = (
|
|
64
|
+
soup.find("h2", class_="quote-header_ticker-wrapper_company").text
|
|
65
|
+
)
|
|
66
|
+
base_info["Company"] = company_name.strip()
|
|
67
|
+
|
|
68
|
+
categories = soup.find("div", class_="quote-header_categories")
|
|
69
|
+
if categories is None:
|
|
70
|
+
categories = soup.find("div", class_="quote-links")
|
|
71
|
+
links = categories.find_all("a") if categories else []
|
|
72
|
+
|
|
73
|
+
for link in links:
|
|
74
|
+
href = link.get("href", "")
|
|
75
|
+
if "f=sec_" in href:
|
|
76
|
+
base_info["Sector"] = link.text
|
|
77
|
+
elif "f=ind_" in href:
|
|
78
|
+
base_info["Industry"] = link.text
|
|
79
|
+
elif "f=geo_" in href:
|
|
80
|
+
base_info["Country"] = link.text
|
|
81
|
+
elif "f=exch_" in href:
|
|
82
|
+
base_info["Exchange"] = link.text
|
|
83
|
+
|
|
84
|
+
return base_info
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _is_float_like(val: str) -> bool:
|
|
88
|
+
try:
|
|
89
|
+
float(val)
|
|
90
|
+
return True
|
|
91
|
+
except ValueError:
|
|
92
|
+
return False
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _convert_value(value: str):
|
|
96
|
+
if value is None:
|
|
97
|
+
return None
|
|
98
|
+
|
|
99
|
+
value = value.strip()
|
|
100
|
+
|
|
101
|
+
if value in ("", "-"):
|
|
102
|
+
return None
|
|
103
|
+
elif value.endswith("%") and _is_float_like(value.strip("%")):
|
|
104
|
+
return float(value.strip("%")) / 100
|
|
105
|
+
elif value.endswith("M") and _is_float_like(value.strip("M")):
|
|
106
|
+
return float(value.strip("M")) * 1e6
|
|
107
|
+
elif value.endswith("B") and _is_float_like(value.strip("B")):
|
|
108
|
+
return float(value.strip("B")) * 1e9
|
|
109
|
+
elif value.endswith("T") and _is_float_like(value.strip("T")):
|
|
110
|
+
return float(value.strip("T")) * 1e12
|
|
111
|
+
elif _is_float_like(value.replace(",", "")):
|
|
112
|
+
return float(value.replace(",", ""))
|
|
113
|
+
else:
|
|
114
|
+
return value
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _convert_date(value: str):
|
|
118
|
+
try:
|
|
119
|
+
return datetime.strptime(value, "%b %d, %Y").date().isoformat()
|
|
120
|
+
except ValueError:
|
|
121
|
+
return _convert_value(value)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _split_two_values(value: str, first_key: str, second_key: str) -> dict:
|
|
125
|
+
values = value.split()
|
|
126
|
+
if len(values) != 2:
|
|
127
|
+
return {first_key: _convert_value(value), second_key: None}
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
first_key: _convert_value(values[0]),
|
|
131
|
+
second_key: _convert_value(values[1]),
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _split_dividend(value: str, amount_key: str, yield_key: str) -> dict:
|
|
136
|
+
values = value.replace("(", "").replace(")", "").split()
|
|
137
|
+
if len(values) != 2:
|
|
138
|
+
return {amount_key: _convert_value(value), yield_key: None}
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
amount_key: _convert_value(values[0]),
|
|
142
|
+
yield_key: _convert_value(values[1]),
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _split_option_short(value: str) -> dict:
|
|
147
|
+
values = [item.strip() for item in value.split("/")]
|
|
148
|
+
if len(values) != 2:
|
|
149
|
+
return {"Optionable": _convert_value(value), "Shortable": None}
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
"Optionable": values[0] == "Yes",
|
|
153
|
+
"Shortable": values[1] == "Yes",
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _split_earnings(value: str) -> dict:
|
|
158
|
+
values = value.split()
|
|
159
|
+
if len(values) < 3:
|
|
160
|
+
return {"Earnings Date": _convert_value(value), "Earnings Time": None}
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
"Earnings Date": " ".join(values[:-1]),
|
|
164
|
+
"Earnings Time": values[-1],
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _convert_to_floats(data_dict: dict) -> dict:
|
|
169
|
+
"""
|
|
170
|
+
Converts values in a dictionary to floats where applicable.
|
|
171
|
+
- Replaces single dash ('-') with None.
|
|
172
|
+
- Splits values representing a range (e.g., '10.86 - 19.08') into two separate keys.
|
|
173
|
+
- Converts values with '%' to proportionate floats.
|
|
174
|
+
- Converts values ending in 'M', 'B', or 'T' to their numeric equivalents in millions, billions, or trillions.
|
|
175
|
+
- Converts numeric strings with commas (e.g., '30,196,932') into floats.
|
|
176
|
+
"""
|
|
177
|
+
|
|
178
|
+
compound_fields = {
|
|
179
|
+
"Volatility": lambda value: _split_two_values(
|
|
180
|
+
value, "Volatility Week", "Volatility Month"
|
|
181
|
+
),
|
|
182
|
+
"52W High": lambda value: _split_two_values(
|
|
183
|
+
value, "52W High Price", "52W High Change"
|
|
184
|
+
),
|
|
185
|
+
"52W Low": lambda value: _split_two_values(
|
|
186
|
+
value, "52W Low Price", "52W Low Change"
|
|
187
|
+
),
|
|
188
|
+
"EPS past 3/5Y": lambda value: _split_two_values(
|
|
189
|
+
value, "EPS past 3Y", "EPS past 5Y"
|
|
190
|
+
),
|
|
191
|
+
"Sales past 3/5Y": lambda value: _split_two_values(
|
|
192
|
+
value, "Sales past 3Y", "Sales past 5Y"
|
|
193
|
+
),
|
|
194
|
+
"Dividend Gr. 3/5Y": lambda value: _split_two_values(
|
|
195
|
+
value, "Dividend Gr. 3Y", "Dividend Gr. 5Y"
|
|
196
|
+
),
|
|
197
|
+
"EPS/Sales Surpr.": lambda value: _split_two_values(
|
|
198
|
+
value, "EPS Surprise", "Sales Surprise"
|
|
199
|
+
),
|
|
200
|
+
"Dividend Est.": lambda value: _split_dividend(
|
|
201
|
+
value, "Dividend Est. Amount", "Dividend Est. Yield"
|
|
202
|
+
),
|
|
203
|
+
"Dividend TTM": lambda value: _split_dividend(
|
|
204
|
+
value, "Dividend TTM Amount", "Dividend TTM Yield"
|
|
205
|
+
),
|
|
206
|
+
"Option/Short": _split_option_short,
|
|
207
|
+
"Earnings": _split_earnings,
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
date_fields = {"IPO", "Dividend Ex-Date"}
|
|
211
|
+
|
|
212
|
+
new_data = {}
|
|
213
|
+
for key, value in data_dict.items():
|
|
214
|
+
value = value.strip() if isinstance(value, str) else value
|
|
215
|
+
|
|
216
|
+
if key in compound_fields:
|
|
217
|
+
new_data.update(compound_fields[key](value))
|
|
218
|
+
elif key in date_fields:
|
|
219
|
+
new_data[key] = _convert_date(value)
|
|
220
|
+
else:
|
|
221
|
+
new_data[key] = _convert_value(value)
|
|
222
|
+
|
|
223
|
+
return new_data
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: finviz-data
|
|
3
|
+
Version: 1.3.1
|
|
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
|
+
By default, the live Finviz test is skipped. To run the full test suite without
|
|
30
|
+
skipping the live test, enable it with `FINVIZ_LIVE_TESTS=1`:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
FINVIZ_LIVE_TESTS=1 uv run python -m unittest discover
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The live test makes a real request to finviz.com and may still be skipped if
|
|
37
|
+
Finviz temporarily rate limits your IP.
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
|
|
43
|
+
from finviz_data import finviz_data
|
|
44
|
+
|
|
45
|
+
# Get the html soup for a single ticker
|
|
46
|
+
soup = finviz_data.get_soup('AAPL')
|
|
47
|
+
|
|
48
|
+
# Get the fundamentals for a single ticker
|
|
49
|
+
fundamentals = finviz_data.get_fundamentals(soup)
|
|
50
|
+
|
|
51
|
+
# Get the fundamentals where all is formatted to float values where possible
|
|
52
|
+
fundamentals = finviz_data.get_fundementals_float(soup)
|
|
53
|
+
|
|
54
|
+
# Get basic company info, sector, ticker etc.
|
|
55
|
+
company_info = finviz_data.get_company_info(soup)
|
|
56
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
finviz_data/__init__.py
|
|
4
|
+
finviz_data/finviz_data.py
|
|
5
|
+
finviz_data.egg-info/PKG-INFO
|
|
6
|
+
finviz_data.egg-info/SOURCES.txt
|
|
7
|
+
finviz_data.egg-info/dependency_links.txt
|
|
8
|
+
finviz_data.egg-info/requires.txt
|
|
9
|
+
finviz_data.egg-info/top_level.txt
|
|
10
|
+
tests/test_finviz_data.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
finviz_data
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "finviz-data"
|
|
3
|
+
version = "1.3.1"
|
|
4
|
+
description = "Simple package to get data from finviz.com"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.9"
|
|
7
|
+
authors = [
|
|
8
|
+
{ name = "Dennis Iversen", email = "dennis.iversen@gmail.com" },
|
|
9
|
+
]
|
|
10
|
+
dependencies = [
|
|
11
|
+
"beautifulsoup4>=4.12,<5",
|
|
12
|
+
"curl-cffi>=0.7,<1",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Repository = "https://github.com/diversen/finviz-data"
|
|
17
|
+
|
|
18
|
+
[build-system]
|
|
19
|
+
requires = ["setuptools>=68"]
|
|
20
|
+
build-backend = "setuptools.build_meta"
|
|
21
|
+
|
|
22
|
+
[tool.setuptools.packages.find]
|
|
23
|
+
include = ["finviz_data*"]
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
from finviz_data import finviz_data
|
|
2
|
+
|
|
3
|
+
# import python unit test
|
|
4
|
+
import os
|
|
5
|
+
import unittest
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from unittest.mock import Mock, patch
|
|
8
|
+
|
|
9
|
+
from bs4 import BeautifulSoup
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
ticker = "AAPL"
|
|
13
|
+
FIXTURE = Path(__file__).resolve().parents[1] / "finviz_example_latest.html"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class TestFinvizData(unittest.TestCase):
|
|
17
|
+
@classmethod
|
|
18
|
+
def setUpClass(cls):
|
|
19
|
+
cls.html = FIXTURE.read_text(encoding="utf-8")
|
|
20
|
+
cls.soup = BeautifulSoup(cls.html, "html.parser")
|
|
21
|
+
|
|
22
|
+
def test_get_soup(self):
|
|
23
|
+
response = Mock(text=self.html, status_code=200)
|
|
24
|
+
with patch("finviz_data.finviz_data.requests.get", return_value=response) as get:
|
|
25
|
+
soup = finviz_data.get_soup(ticker)
|
|
26
|
+
|
|
27
|
+
get.assert_called_once_with(
|
|
28
|
+
"https://finviz.com/stock?t=AAPL&p=d",
|
|
29
|
+
impersonate="chrome",
|
|
30
|
+
)
|
|
31
|
+
self.assertIsNotNone(soup)
|
|
32
|
+
self.assertEqual(soup.find("h1").get_text(strip=True), ticker)
|
|
33
|
+
|
|
34
|
+
def test_get_soup_raises_for_http_error(self):
|
|
35
|
+
response = Mock(text="Not found", status_code=404)
|
|
36
|
+
with patch("finviz_data.finviz_data.requests.get", return_value=response):
|
|
37
|
+
with self.assertRaises(finviz_data.FinvizRequestError) as error:
|
|
38
|
+
finviz_data.get_soup("BYDN")
|
|
39
|
+
|
|
40
|
+
message = str(error.exception)
|
|
41
|
+
self.assertIn("BYDN", message)
|
|
42
|
+
self.assertIn("HTTP 404", message)
|
|
43
|
+
self.assertIn("https://finviz.com/stock?t=BYDN&p=d", message)
|
|
44
|
+
|
|
45
|
+
@unittest.skipUnless(
|
|
46
|
+
os.getenv("FINVIZ_LIVE_TESTS") == "1",
|
|
47
|
+
"set FINVIZ_LIVE_TESTS=1 to run live Finviz tests",
|
|
48
|
+
)
|
|
49
|
+
def test_get_soup_live(self):
|
|
50
|
+
soup = finviz_data.get_soup(ticker)
|
|
51
|
+
page_text = soup.get_text(" ", strip=True)
|
|
52
|
+
|
|
53
|
+
if "temporarily rate limited" in page_text:
|
|
54
|
+
self.skipTest("Finviz temporarily rate limited this IP")
|
|
55
|
+
|
|
56
|
+
self.assertIsNotNone(soup)
|
|
57
|
+
table = soup.find("table", class_="snapshot-table2")
|
|
58
|
+
self.assertIsNotNone(table, page_text[:500])
|
|
59
|
+
|
|
60
|
+
header = soup.find("h1", class_="quote-header_ticker-wrapper_ticker")
|
|
61
|
+
self.assertIsNotNone(header, page_text[:500])
|
|
62
|
+
self.assertEqual(header.get_text(strip=True), ticker)
|
|
63
|
+
|
|
64
|
+
fundamentals = finviz_data.get_fundamentals(soup)
|
|
65
|
+
self.assertIn("Market Cap", fundamentals)
|
|
66
|
+
self.assertIn("P/E", fundamentals)
|
|
67
|
+
|
|
68
|
+
company_info = finviz_data.get_company_info(soup)
|
|
69
|
+
self.assertEqual(company_info["Ticker"], ticker)
|
|
70
|
+
self.assertIn("Company", company_info)
|
|
71
|
+
|
|
72
|
+
@unittest.skipUnless(
|
|
73
|
+
os.getenv("FINVIZ_LIVE_TESTS") == "1",
|
|
74
|
+
"set FINVIZ_LIVE_TESTS=1 to run live Finviz tests",
|
|
75
|
+
)
|
|
76
|
+
def test_get_soup_live_raises_for_invalid_ticker(self):
|
|
77
|
+
with self.assertRaises(finviz_data.FinvizRequestError) as error:
|
|
78
|
+
finviz_data.get_soup("BYDN")
|
|
79
|
+
|
|
80
|
+
message = str(error.exception)
|
|
81
|
+
self.assertIn("BYDN", message)
|
|
82
|
+
self.assertIn("HTTP 404", message)
|
|
83
|
+
self.assertIn("https://finviz.com/stock?t=BYDN&p=d", message)
|
|
84
|
+
|
|
85
|
+
def test_get_fundamentals(self):
|
|
86
|
+
fundamentals = finviz_data.get_fundamentals(self.soup)
|
|
87
|
+
self.assertIsInstance(fundamentals, dict)
|
|
88
|
+
self.assertEqual(fundamentals["Market Cap"], "4183.77B")
|
|
89
|
+
self.assertEqual(fundamentals["P/E"], "34.46")
|
|
90
|
+
self.assertEqual(fundamentals["Volatility"], "3.31% 2.71%")
|
|
91
|
+
|
|
92
|
+
def test_get_company_info(self):
|
|
93
|
+
company_info = finviz_data.get_company_info(self.soup)
|
|
94
|
+
self.assertIsInstance(company_info, dict)
|
|
95
|
+
|
|
96
|
+
# check if company_info has all the keys
|
|
97
|
+
self.assertEqual(company_info["Ticker"], ticker)
|
|
98
|
+
self.assertEqual(company_info["Company"], "Apple Inc")
|
|
99
|
+
self.assertEqual(company_info["Sector"], "Technology")
|
|
100
|
+
self.assertEqual(company_info["Industry"], "Consumer Electronics")
|
|
101
|
+
self.assertEqual(company_info["Country"], "USA")
|
|
102
|
+
self.assertEqual(company_info["Exchange"], "NASD")
|
|
103
|
+
self.assertIn("Company", company_info)
|
|
104
|
+
self.assertIn("Sector", company_info)
|
|
105
|
+
self.assertIn("Industry", company_info)
|
|
106
|
+
self.assertIn("Country", company_info)
|
|
107
|
+
self.assertIn("Exchange", company_info)
|
|
108
|
+
|
|
109
|
+
def test_get_get_fundamentals_converted(self):
|
|
110
|
+
fundamentals_converted = finviz_data.get_fundamentals_float(self.soup)
|
|
111
|
+
self.assertIsInstance(fundamentals_converted, dict)
|
|
112
|
+
self.assertAlmostEqual(
|
|
113
|
+
fundamentals_converted["Market Cap"], 4183.77e9, delta=1
|
|
114
|
+
)
|
|
115
|
+
self.assertEqual(fundamentals_converted["P/E"], 34.46)
|
|
116
|
+
self.assertAlmostEqual(fundamentals_converted["Volatility Week"], 0.0331)
|
|
117
|
+
self.assertAlmostEqual(fundamentals_converted["Volatility Month"], 0.0271)
|
|
118
|
+
self.assertEqual(fundamentals_converted["52W High Price"], 317.4)
|
|
119
|
+
self.assertAlmostEqual(fundamentals_converted["52W High Change"], -0.1025)
|
|
120
|
+
self.assertEqual(fundamentals_converted["52W Low Price"], 199.26)
|
|
121
|
+
self.assertAlmostEqual(fundamentals_converted["52W Low Change"], 0.4296)
|
|
122
|
+
self.assertAlmostEqual(fundamentals_converted["EPS past 3Y"], 0.0689)
|
|
123
|
+
self.assertAlmostEqual(fundamentals_converted["EPS past 5Y"], 0.1791)
|
|
124
|
+
self.assertAlmostEqual(fundamentals_converted["Sales past 3Y"], 0.0181)
|
|
125
|
+
self.assertAlmostEqual(fundamentals_converted["Sales past 5Y"], 0.0871)
|
|
126
|
+
self.assertAlmostEqual(fundamentals_converted["Dividend Gr. 3Y"], 0.0426)
|
|
127
|
+
self.assertAlmostEqual(fundamentals_converted["Dividend Gr. 5Y"], 0.0498)
|
|
128
|
+
self.assertEqual(fundamentals_converted["Dividend Est. Amount"], 1.08)
|
|
129
|
+
self.assertAlmostEqual(fundamentals_converted["Dividend Est. Yield"], 0.0038)
|
|
130
|
+
self.assertEqual(fundamentals_converted["Dividend TTM Amount"], 1.05)
|
|
131
|
+
self.assertAlmostEqual(fundamentals_converted["Dividend TTM Yield"], 0.0037)
|
|
132
|
+
self.assertEqual(fundamentals_converted["Optionable"], True)
|
|
133
|
+
self.assertEqual(fundamentals_converted["Shortable"], True)
|
|
134
|
+
self.assertAlmostEqual(fundamentals_converted["EPS Surprise"], 0.033)
|
|
135
|
+
self.assertAlmostEqual(fundamentals_converted["Sales Surprise"], 0.0158)
|
|
136
|
+
self.assertEqual(fundamentals_converted["Earnings Date"], "Apr 30")
|
|
137
|
+
self.assertEqual(fundamentals_converted["Earnings Time"], "AMC")
|
|
138
|
+
self.assertEqual(fundamentals_converted["IPO"], "1980-12-12")
|
|
139
|
+
self.assertEqual(fundamentals_converted["Dividend Ex-Date"], "2026-05-11")
|
|
140
|
+
self.assertIsNone(fundamentals_converted["Trades"])
|
|
141
|
+
|
|
142
|
+
self.assertNotIn("Volatility", fundamentals_converted)
|
|
143
|
+
self.assertNotIn("52W High", fundamentals_converted)
|
|
144
|
+
self.assertNotIn("52W Low", fundamentals_converted)
|
|
145
|
+
self.assertNotIn("EPS past 3/5Y", fundamentals_converted)
|
|
146
|
+
self.assertNotIn("Sales past 3/5Y", fundamentals_converted)
|
|
147
|
+
self.assertNotIn("Dividend Gr. 3/5Y", fundamentals_converted)
|
|
148
|
+
self.assertNotIn("Dividend Est.", fundamentals_converted)
|
|
149
|
+
self.assertNotIn("Dividend TTM", fundamentals_converted)
|
|
150
|
+
self.assertNotIn("Option/Short", fundamentals_converted)
|
|
151
|
+
self.assertNotIn("EPS/Sales Surpr.", fundamentals_converted)
|
|
152
|
+
self.assertNotIn("Earnings", fundamentals_converted)
|
|
153
|
+
|
|
154
|
+
def test_convert_percentage(self):
|
|
155
|
+
value = "1.39%"
|
|
156
|
+
converted_value = finviz_data._convert_value(value)
|
|
157
|
+
self.assertEqual(converted_value, 0.0139)
|
|
158
|
+
|
|
159
|
+
def test_convert_million(self):
|
|
160
|
+
value = "1.39M"
|
|
161
|
+
converted_value = finviz_data._convert_value(value)
|
|
162
|
+
self.assertEqual(converted_value, 1390000)
|
|
163
|
+
|
|
164
|
+
def test_convert_negative_number(self):
|
|
165
|
+
value = "-1.39"
|
|
166
|
+
converted_value = finviz_data._convert_value(value)
|
|
167
|
+
self.assertEqual(converted_value, -1.39)
|
|
168
|
+
|
|
169
|
+
def test_convert_empty_value(self):
|
|
170
|
+
self.assertIsNone(finviz_data._convert_value(""))
|
|
171
|
+
self.assertIsNone(finviz_data._convert_value("-"))
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
if __name__ == "__main__":
|
|
175
|
+
unittest.main()
|