Skip to content

Instantly share code, notes, and snippets.

@juanifioren
Created January 31, 2018 17:19
Show Gist options
  • Save juanifioren/9766edf80cb3d71e6a68517898144cee to your computer and use it in GitHub Desktop.
Save juanifioren/9766edf80cb3d71e6a68517898144cee to your computer and use it in GitHub Desktop.
Stock Finantial Data From Yahoo
from lxml import html
import requests
def get_stock_finantial_data(symbol):
url = 'https://finance.yahoo.com/quote/{symbol}/'
r = requests.get(url.format(symbol=symbol))
tree = html.fromstring(r.content)
to_search = [
'PREV_CLOSE', 'OPEN', 'BID', 'ASK', 'DAYS_RANGE', 'FIFTY_TWO_WK_RANGE',
'TD_VOLUME', 'AVERAGE_VOLUME_3MONTH', 'MARKET_CAP', 'BETA', 'PE_RATIO',
'EPS_RATIO', 'EARNINGS_DATE', 'DIVIDEND_AND_YIELD', 'EXDIVIDEND_DATE',
'ONE_YEAR_TARGET_PRICE',
]
data = {}
for key in to_search:
try:
data[key] = tree.cssselect('td[data-test="'+key+'-value"] span')[0].text_content()
except IndexError:
data[key] = None
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment