'''Scrape income statement from Yahoo Finance for a given ticker
@param: ticker
'''
income_site = "https://finance.yahoo.com/quote/" + ticker + "/financials?p=" + ticker
response = requests.get(income_site)
html = response.text
print(f"html: {html}")
# Parse the list of scripts using a regular expression pattern
pattern = r'root\.App\.main = (.*?);\n}(this));'
script_data = re.search(pattern, html, flags=re.DOTALL).group(1)
json_info = json.loads(script_data)
if yearly:
temp = json_info["context"]["dispatcher"]["stores"]["QuoteSummaryStore"]["incomeStatementHistory"]["incomeStatementHistory"]
else:
temp = json_info["context"]["dispatcher"]["stores"]["QuoteSummaryStore"]["incomeStatementHistoryQuarterly"]["incomeStatementHistory"]
return _parse_table(temp)
But i got always the TypeError: string indices must be integers
I try to correct the code :
def get_income_statement(ticker, yearly = True):