Notes
Notes - notes.io |
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import pandas as pd
chrome_path = r"C:UsersJawaharDesktopchromedriver_win32chromedriver.exe"
s = Service(chrome_path)
driver = webdriver.Chrome(service=s)
def get_data_from_yahooFinance(url_link,ticker):
driver.get(url_link)
time.sleep(2)
#For hiding the popup coming to the screen
if(len(driver.find_elements(By.XPATH,"//button[text()='Maybe later']")) > 0):
hide_popup=driver.find_element(By.XPATH,"//button[text()='Maybe later']")
ActionChains(driver).click(hide_popup).perform()
#Quarterly
quarter=driver.find_element(By.XPATH,"//span[text()='Quarterly']")
ActionChains(driver).click(quarter).perform()
#For expanding all columns
expand=driver.find_element(By.XPATH,"//span[text()='Expand All']")
ActionChains(driver).click(expand).perform()
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//div[@class="D(tbhg)"]')))
headers_elem = driver.find_elements(By.XPATH, '//div[@class="D(tbhg)"]/div/div')
col_headers = [header.text for header in headers_elem]
df = pd.DataFrame(columns = col_headers)
rows = driver.find_element(By.XPATH, '//div[@class="D(tbrg)"]')
rows_text=[row_value for row_value in rows.text.split("n")]
for i in range(len(rows_text)):
if(i%2 != 0):
row_data=[]
row_data.append(rows_text[i-1])
row_data+=rows_text[i].split(" ")
new_row_data = []
for i in row_data:
data = i.replace(',',"")
data1 = string_to_int(data)
#print(str(data1) + str(type(data1)))
new_row_data.append(data1)
df.loc[len(df)] = new_row_data
df['Ticker'] = ticker
return df
#string to date time &
#date time conversion to year
def string_to_int(s):
if not s:
return s
try:
f = float(s)
i = int(f)
return i if f == i else f
except ValueError:
return s
def get_tickers_lists():
fh = open("tickers.txt")
tickers_string = fh.read()
tickers_list = tickers_string.split("n")
tickers_list = list(filter(None,tickers_list))
return tickers_list
def build_income_statement_url(ticker):
url=f"https://finance.yahoo.com/quote/{ticker}/financials?p={ticker}"
return url
def build_balance_sheet_url(ticker):
url=f"https://finance.yahoo.com/quote/{ticker}/balance-sheet?p={ticker}"
return url
def build_cash_flow_url(ticker):
url=f"https://finance.yahoo.com/quote/{ticker}/cash-flow?p={ticker}"
return url
def write_to_excel(df,filename):
df.to_excel(filename)
def get_income_statement_data():
list_of_tickers=get_tickers_lists()
df=pd.DataFrame()
for ticker in list_of_tickers:
income_statement_url=build_income_statement_url(ticker)
df1=get_data_from_yahooFinance(income_statement_url,ticker)
df=pd.concat([df,df1],axis=0,ignore_index=True)
#print(df)
write_to_excel(df,'income_statement_sample.xlsx')
def get_balance_sheet_data():
list_of_tickers=get_tickers_lists()
df=pd.DataFrame()
for ticker in list_of_tickers:
balance_sheet_url=build_balance_sheet_url(ticker)
df1=get_data_from_yahooFinance(balance_sheet_url,ticker)
df=pd.concat([df,df1],axis=0,ignore_index=True)
#print(df)
write_to_excel(df,'balance_sheet.xlsx')
def get_cash_flow_data():
list_of_tickers=get_tickers_lists()
df=pd.DataFrame()
for ticker in list_of_tickers:
cash_flow_url=build_cash_flow_url(ticker)
df1=get_data_from_yahooFinance(cash_flow_url,ticker)
df=pd.concat([df,df1],axis=0,ignore_index=True)
#print(df)
write_to_excel(df,'cash_flow.xlsx')
def main():
get_income_statement_data()
# get_balance_sheet_data()
# get_cash_flow_data()
print("--------Completed--------")
main()
TEST FILE
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import pandas as pd
chrome_path = r"C:UsersJawaharDesktopchromedriver_win32chromedriver.exe"
s = Service(chrome_path)
driver = webdriver.Chrome(service=s)
def get_data_from_yahooFinance(url_link,ticker):
driver.get(url_link)
time.sleep(2)
#For hiding the popup coming to the screen
if(len(driver.find_elements(By.XPATH,"//button[text()='Maybe later']")) > 0):
hide_popup=driver.find_element(By.XPATH,"//button[text()='Maybe later']")
ActionChains(driver).click(hide_popup).perform()
#Quarterly
quarter=driver.find_element(By.XPATH,"//span[text()='Quarterly']")
ActionChains(driver).click(quarter).perform()
#For expanding all columns
expand=driver.find_element(By.XPATH,"//span[text()='Expand All']")
ActionChains(driver).click(expand).perform()
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//div[@class="D(tbhg)"]')))
headers_elem = driver.find_elements(By.XPATH, '//div[@class="D(tbhg)"]/div/div')
col_headers = [header.text for header in headers_elem]
df = pd.DataFrame(columns = col_headers)
rows = driver.find_element(By.XPATH, '//div[@class="D(tbrg)"]')
rows_text=[row_value for row_value in rows.text.split("n")]
for i in range(len(rows_text)):
if(i%2 != 0):
row_data=[]
row_data.append(rows_text[i-1])
row_data+=rows_text[i].split(" ")
df.loc[len(df)] = row_data
df['Ticker'] = ticker
return df
def get_tickers_lists():
fh = open("tickers.txt")
tickers_string = fh.read()
tickers_list = tickers_string.split("n")
tickers_list = list(filter(None,tickers_list))
return tickers_list
def build_income_statement_url(ticker):
url=f"https://finance.yahoo.com/quote/{ticker}/financials?p={ticker}"
return url
def build_balance_sheet_url(ticker):
url=f"https://finance.yahoo.com/quote/{ticker}/balance-sheet?p={ticker}"
return url
def build_cash_flow_url(ticker):
url=f"https://finance.yahoo.com/quote/{ticker}/cash-flow?p={ticker}"
return url
def write_to_excel(df,filename):
df.to_excel(filename)
def get_income_statement_data():
list_of_tickers=get_tickers_lists()
df=pd.DataFrame()
for ticker in list_of_tickers:
income_statement_url=build_income_statement_url(ticker)
df1=get_data_from_yahooFinance(income_statement_url,ticker)
df=pd.concat([df,df1],axis=0,ignore_index=True)
#print(df)
write_to_excel(df,'income_statement.xlsx')
def get_balance_sheet_data():
list_of_tickers=get_tickers_lists()
df=pd.DataFrame()
for ticker in list_of_tickers:
balance_sheet_url=build_balance_sheet_url(ticker)
df1=get_data_from_yahooFinance(balance_sheet_url,ticker)
df=pd.concat([df,df1],axis=0,ignore_index=True)
#print(df)
write_to_excel(df,'balance_sheet.xlsx')
def get_cash_flow_data():
list_of_tickers=get_tickers_lists()
df=pd.DataFrame()
for ticker in list_of_tickers:
cash_flow_url=build_cash_flow_url(ticker)
df1=get_data_from_yahooFinance(cash_flow_url,ticker)
df=pd.concat([df,df1],axis=0,ignore_index=True)
#print(df)
write_to_excel(df,'cash_flow.xlsx')
def main():
get_income_statement_data()
#get_balance_sheet_data()
#get_cash_flow_data()
print("--------Completed--------")
main()
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team