Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix broken functions caused by changes to the yahoo api #119

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions yahoo_fin.egg-info/requires.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ requests_html
feedparser
requests
pandas
pycryptodome
beautifulsoup4==4.11.1
bs4==0.0.2
40 changes: 22 additions & 18 deletions yahoo_fin/stock_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from Crypto.Util.Padding import unpad
# For pretty print
from pprint import pp
from bs4 import BeautifulSoup


try:
Expand Down Expand Up @@ -446,37 +447,40 @@ def _parse_json(url, headers = {'User-agent': 'Mozilla/5.0'}):

html = requests.get(url=url, headers = headers).text

json_str = html.split('root.App.main =')[1].split('(this)')[0].split(';\n}')[0].strip()
json_str = '{}'

soup = BeautifulSoup(html, "html.parser")
script_tags = soup.find_all('script')
for script_tag in script_tags:
data_url = script_tag.get('data-url')
if data_url and "quoteSummary" in data_url:
if script_tag.contents is not None and len(script_tag.contents):
json_str = script_tag.contents[0]

try:
data = json.loads(json_str)
#print("type of json_str :", type(data))
unencrypted_stores = _decrypt_yblob_aes(data)
json_info = unencrypted_stores['QuoteSummaryStore']
#print("json_info :", json_info)
json_info = data
body_json = json.loads(json_info["body"])
result = body_json["quoteSummary"]["result"][0]
except:
return '{}'
#else:
# return data
#new_data = json.dumps(data).replace('{}', 'null')
#new_data = re.sub(r'\{[\'|\"]raw[\'|\"]:(.*?),(.*?)\}', r'\1', new_data)
#json_info = json.loads(new_data)
#print("json info :", json_info)
return json_info

return result

def _parse_table(json_info):

def _parse_table(json_info):
df = pd.DataFrame(json_info)

if df.empty:
return df

del df["maxAge"]

df.set_index("endDate", inplace=True)
df["rawEndDate"] = df["endDate"][0]["raw"]

df.set_index("rawEndDate", inplace=True)
df.index = pd.to_datetime(df.index, unit="s")

df = df.transpose()
df.index.name = "Breakdown"

Expand Down Expand Up @@ -887,7 +891,7 @@ def get_earnings(ticker):

result["quarterly_revenue_earnings"] = pd.DataFrame.from_dict(temp["financialsChart"]["quarterly"])

return (pp(result))
return result



Expand Down