I'm trying to make a graph that plots real time stock prices from yahoo finance using matplotlib and python 3.5. My code is as follows:
import urllib.request
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import datetime
import time
t = urllib.request.urlopen('http://finance.yahoo.com/quote/YHOO/history?ltr=1')
the = t.read()
soup = BeautifulSoup(the, 'html.parser')
timeticks = []
x = []
prices = []
i = 1
while 1:
n = soup.find('span',class_="Fw(500) D(ib) Fz(42px)")
time.sleep(1)
plt.cla()
nf = float(n.string)
prices.append(nf)
now= datetime.datetime.now()
times=('%02d:%02d:%d'%(now.hour,now.minute,now.second))
timeticks.append(times)
x.append(i)
print(timeticks)
print(x)
print(prices)
plt.xticks(x,timeticks)
plt.plot(x,prices,"b-")
plt.xlabel("Time")
plt.ylabel("Stock prices")
plt.title("Yahoo's Stocks")
plt.show()
i +=1
I know that some of the code might seem wrong(I am still learning).
When I ran this code, the plot doesn't show up. I added print statements to check if the lists were working and they were.
print(timeticks)
print(x)
print(prices)
So what do I need to do to make my code working? Thank You