程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

雅虎股票 API 导致 Python 中的某些股票出现错误,其中除了函数不起作用

发布于2022-10-31 23:43     阅读(1067)     评论(0)     点赞(7)     收藏(4)


I use the Yahoo API a lot to get stock data from multiple exchanges in the world. The code normally works, but it look likes that there has been an update in the API. In the past when a stock was delisted, the except function in Python got the error and continued. The strange thing is that this error is caused by only some stocks, which are delisted?

For instance stock ´ANTM´, this stock is delisted. I receive this error while trying to scrape the stock data: AttributeError: 'float' object has no attribute 'upper'. This code runs till infinity.

 !pip install yfinance
 import yfinance as yf
 import datetime as dt
 start = '2021-01-01'
 end =  dt.datetime.today().strftime('%Y-%m-%d')
            
 tickers=['AAPL', 'ANTM']
            
 for ticker in tickers:
                try:
                    df=yf.download(ticker, start, end, progress=False)
                    df.index = df.index.strftime('%Y/%m/%d')
                    df.to_csv(f'{ticker}.csv')
            
                except Exception: 
                    if ticker not in tickers:
                     continue

I have created a workaround, but this is slow for 1K of stocks.

 #code with workaround
        
  tickers=['AAPL','ANTM']
        
  for ticker in tickers:
            try:
               if yf.Ticker(ticker).info['regularMarketPrice']!=None:  #workaround
                df=yf.download(ticker, start, end, progress=False)
                df.index = df.index.strftime('%Y/%m/%d')
                df.to_csv(f'{ticker}.csv')
               else:   #workaround
                 continue #workaround
            except Exception: 
                if ticker not in tickers:
                 continue

我该怎么做才能确保代码捕捉到错误并继续执行代码,这不会影响代码的速度?


解决方案


暂无回答



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/1835043/122343100d6e3684d11e/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

7 0
收藏该文
已收藏

评论内容:(最多支持255个字符)