Created
July 28, 2018 22:34
-
-
Save ScriptAutomate/1e597376250ea6d91321f2fea2a2e3bc to your computer and use it in GitHub Desktop.
Basic stock worth via Yahoo API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mechanicalsoup | |
import argparse | |
def get_ticker(): | |
""" | |
Grab user supplied arguments using the | |
argparse library. | |
""" | |
parser = argparse.ArgumentParser( | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | |
description="%(prog)s is a tool for checking \ | |
the current worth of a stock via the Yahoo Finance \ | |
API." | |
) | |
parser.add_argument('-t', | |
'--ticker', | |
dest='ticker', | |
required=True, | |
help='Ticker name on the stock exchange', | |
type=str) | |
args = parser.parse_args() | |
web_page = soup_cook(args.ticker) | |
return web_page | |
def soup_cook(ticker): | |
stock_base_url = "http://finance.yahoo.com/q?s=" | |
web_browser = mechanicalsoup.Browser() | |
web_page = web_browser.get(f"{stock_base_url}{ticker}") | |
return web_page | |
def stock_server(web_page): | |
results = {} | |
results['ticker_price'] = web_page.soup.find_all( | |
"span", attrs={"data-reactid": "21"})[1].text | |
results['ticker_diff'] = web_page.soup.find_all( | |
"span", attrs={"data-reactid": "23"})[0].text | |
results['ticker_time'] = web_page.soup.find_all( | |
"span", attrs={"data-reactid": "25"})[2].text | |
return results | |
if __name__ == "__main__": | |
ticker = get_ticker() | |
stock_result = stock_server(ticker) | |
print(f"[Current Value] {stock_result['ticker_price']}") | |
print(f"[Daily Diff] {stock_result['ticker_diff']}") | |
print(f"[As Of] {stock_result['ticker_time']}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment