Skip to content

Commit 10a4363

Browse files
committed
Fix #2570: Add HTML scraper fallback for missing pegRatio
1 parent d12c4b3 commit 10a4363

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

yfinance/scrapers/quote.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import numpy as _np
55
import pandas as pd
6+
from io import StringIO
67

78
from yfinance import utils
89
from yfinance.config import YfConfig
@@ -716,6 +717,28 @@ def _fetch_complementary(self):
716717
self._info[k] = keydict[k][-1]["reportedValue"]["raw"]
717718
else:
718719
self.info[k] = None
720+
if self._info.get('pegRatio') is None:
721+
try:
722+
url = f"https://finance.yahoo.com/quote/{self._symbol}/key-statistics"
723+
response = self._data.cache_get(url=url)
724+
725+
dfs = pd.read_html(StringIO(response.text))
726+
727+
for df in dfs:
728+
if df.shape[1] >= 2:
729+
mask = df.iloc[:, 0].astype(str).str.contains("PEG Ratio", case=False, na=False)
730+
731+
if mask.any():
732+
val_raw = df.loc[mask, df.columns[1]].iloc[0]
733+
734+
if val_raw and str(val_raw).lower() != 'n/a':
735+
try:
736+
self._info['pegRatio'] = float(val_raw)
737+
except ValueError:
738+
pass
739+
break
740+
except Exception:
741+
pass
719742

720743
def _fetch_calendar(self):
721744
# secFilings return too old data, so not requesting it for now

0 commit comments

Comments
 (0)