Stock prices from Yahoo

posted May 24, 2016, 9:48 AM by Attila Lengyel
from urllib.request import urlopen
import json
import time

symbol = input("Give me a stock symbol:")
sURL = "http://finance.yahoo.com/webservice/v1/symbols/"
sURL += symbol
sURL += "/quote?format=json&view=detail"

while True:
    html = urlopen(sURL)
    response = html.read()
    html.close
    json_data = json.loads(response.decode())
    stock = json_data['list']['resources'][0]['resource']['fields']
    print(symbol, " price is ", stock["price"])
    for key in stock:
        print(key, "=", stock[key])
    
    if float(stock["chg_percent"]) < 0:
        print("Your stock is falling!!!! Sorry....")
    else:
        print("You are making money!!!")
    time.sleep(5)
Comments