-1

I am trying to make the below short code to work. My focus is in fetch_historical_yahoo which seems not to be working. I am trying to use it in a larger code.

import datetime
import matplotlib.finance as finance
import matplotlib.mlab as mlab

startdate = datetime.date(2005,1,1)
today = enddate = datetime.date.today()
ticker = 'nvda'

fh = finance.fetch_historical_yahoo(ticker, startdate, enddate)

r = mlab.csv2rec(fh); fh.close()

r.sort()

When I run the code, I get the below error. When I go and open finance.py I can't seem to be able to place my finger on the url issue.

Any ideas?

I tried mlp_finance but I was nopt able to install it.

fh = finance.fetch_historical_yahoo(ticker, startdate, enddate)

Traceback (most recent call last):

  File "<ipython-input-61-e83eb3d28a19>", line 1, in <module>
    fh = finance.fetch_historical_yahoo(ticker, startdate, enddate)

  File "C:\Users\dvargas\Anaconda3\lib\site-packages\matplotlib\finance.py", line 362, in fetch_historical_yahoo
    with contextlib.closing(urlopen(url)) as urlfh:

  File "C:\Users\dvargas\Anaconda3\lib\urllib\request.py", line 163, in urlopen
    return opener.open(url, data, timeout)

  File "C:\Users\dvargas\Anaconda3\lib\urllib\request.py", line 466, in open
    response = self._open(req, data)

  File "C:\Users\dvargas\Anaconda3\lib\urllib\request.py", line 484, in _open
    '_open', req)

  File "C:\Users\dvargas\Anaconda3\lib\urllib\request.py", line 444, in _call_chain
    result = func(*args)

  File "C:\Users\dvargas\Anaconda3\lib\urllib\request.py", line 1282, in http_open
    return self.do_open(http.client.HTTPConnection, req)

  File "C:\Users\dvargas\Anaconda3\lib\urllib\request.py", line 1256, in do_open
    raise URLError(err)

URLError: <urlopen error [Errno 11004] getaddrinfo failed>
Daniel Vargas
  • 980
  • 2
  • 13
  • 21
  • 1
    Are you behind a firewall? [opening websites using urllib2 from behind corporate firewall - 11004 getaddrinfo failed](https://stackoverflow.com/questions/4847649/opening-websites-using-urllib2-from-behind-corporate-firewall-11004-getaddrinf) and [urllib2.URLError: ](https://stackoverflow.com/questions/5022945/urllib2-urlerror-urlopen-error-errno-11004-getaddrinfo-failed) has some debugging tips – chickity china chinese chicken Nov 07 '17 at 01:44
  • Not the issue really. It seems the moduel is not effective anymore due to yahoo changing it's frame. I did find a workaround. – Daniel Vargas Nov 07 '17 at 03:40

1 Answers1

1

I had to use a workaround.

from urllib.request import urlopen
from bs4 import BeautifulSoup as bs

def get_historical_data(name, number_of_days):
    data = []
    url = "https://finance.yahoo.com/quote/" + name + "/history/"
    rows = bs(urlopen(url).read()).findAll('table')[0].tbody.findAll('tr')

    for each_row in rows:
        divs = each_row.findAll('td')
        if divs[1].span.text  != 'Dividend': #Ignore this row in the table
            #I'm only interested in 'Open' price; For other values, play with divs[1 - 5]
            data.append({'Date': divs[0].span.text, 'Open': float(divs[1].span.text.replace(',',''))})

    return data[:number_of_days]

#Test
for i in get_historical_data('googl', 5):   
    print(i)
Daniel Vargas
  • 980
  • 2
  • 13
  • 21