Python and Yahoo finance weird “YQLQueryError(response['error']['description'])” get_historical

前端 未结 1 927
孤城傲影
孤城傲影 2020-12-07 03:12

I am getting some very weird results using Python and yahoo-finance package. When I run my function on individual stock symbols the function performs OK. But if I put them i

相关标签:
1条回答
  • 2020-12-07 03:43

    It looks like this functionality has been discontinued: https://forums.yahoo.net/t5/Yahoo-Finance-help/ichart-not-working-in-my-app/td-p/253560

    Here's a workaround (grab the data directly from the history pages):

    import urllib2
    from BeautifulSoup import BeautifulSoup as bs
    
    def get_historical_data(name, number_of_days):
        data = []
        url = "https://finance.yahoo.com/quote/" + name + "/history/"
        rows = bs(urllib2.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('amzn', 5):
        print i
    

    Output:

    {'Date': u'Jun 02, 2017', 'Open': 999.0}
    {'Date': u'Jun 01, 2017', 'Open': 998.59}
    {'Date': u'May 31, 2017', 'Open': 1000.0}
    {'Date': u'May 30, 2017', 'Open': 996.51}
    {'Date': u'May 26, 2017', 'Open': 995.0}
    
    0 讨论(0)
提交回复
热议问题