微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pandas & requests 财务分析代码给出 KeyError:0

如何解决Pandas & requests 财务分析代码给出 KeyError:0

我一直在寻找更多工具来自动进行股票分析,这就是我找到以下代码链接的方式。作者说他发布了整个代码,但我没有看到它,所以我正在重建它并且无法让它运行。链接如下。

请求、网页抓取和 Pandas 是我不太精通的领域,所以我认为 Jedi 在 SO 上的代码可以帮助解开或更新此代码https://medium.com/swlh/automating-your-stock-portfolio-research-with-python-for-beginners-912dc02bf1c2

从长远来看,我正在尝试通过在其他人创建的工具中更新或构建更多功能来学习 Python,因此这也是一种学习经验。所以我希望你能修复它,但我更希望你能给出提示并引导我找到可能的解决方案。

# FILENAME financial_analysis.py
# SOURCE https://medium.com/swlh/automating-your-stock-portfolio-research-with-python-for-beginners-912dc02bf1c2

import requests
import pandas as pd


def getdata(stock):
    """Company Quote Group of Items"""
    company_quote = requests.get(f"https://financialmodelingprep.com/api/v3/quote/{stock}")
    company_quote = company_quote.json()
    share_price = float("{0:.2f}".format(company_quote[0]['price']))

    # Balance Sheet Group of Items
    BS = requests.get(f"https://financialmodelingprep.com/api/v3/financials/balance-sheet-statement/{stock}?period=quarter")
    BS = BS.json()

    # print_data = getdata(aapl)
    #Total Debt
    debt = float("{0:.2f}".format(float(BS['financials'][0]['Total debt'])/10**9))#Total Cash
    cash = float("{0:.2f}".format(float(BS['financials'][0]['Cash and short-term investments'])/10**9))

    # Income Statement Group of Items
    IS = requests.get(f"https://financialmodelingprep.com/api/v3/financials/income-statement/{stock}?period=quarter")
    IS = IS.json()

    # Most Recent Quarterly Revenue
    qRev = float("{0:.2f}".format(float(IS['financials'][0]['Revenue'])/10**9))

    # Company Profile Group of Items
    company_info = requests.get(f"https://financialmodelingprep.com/api/v3/company/profile/{stock}")
    company_info = company_info.json()# Chief Executive Officer
    ceo = company_info['profile']['ceo']

    return(share_price,cash,debt,qRev,ceo)


tickers = {'AAPL','MSFT','GOOG','T','CSCO','INTC','ORCL','AMZN','FB','TSLA','NVDA'}

data = map(getdata,tickers)

df = pd.DataFrame(data,columns=['Total Cash','Total Debt','Q3 2019 Revenue','CEO'],index=tickers),print(df)

产生这个错误

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/core/interactiveshell.py",line 3296,in run_code
    exec(code_obj,self.user_global_ns,self.user_ns)
  File "<ipython-input-2-d9759a746769>",line 1,in <module>
    runfile('/Users/owner/sBox/Jamesmk6_3/toolBox/financial_analysis.py',wdir='/Users/owner/sBox/Jamesmk6_3/toolBox')
  File "/Users/owner/Library/Application Support/JetBrains/ToolBox/apps/PyCharm-P/ch-1/193.7288.30/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py",line 197,in runfile
    pydev_imports.execfile(filename,global_vars,local_vars)  # execute the script
  File "/Users/owner/Library/Application Support/JetBrains/ToolBox/apps/PyCharm-P/ch-1/193.7288.30/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py",line 18,in execfile
    exec(compile(contents+"\n",file,'exec'),glob,loc)
  File "/Users/owner/sBox/Jamesmk6_3/toolBox/financial_analysis.py",line 44,in <module>
    index=tickers),print(df)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/frame.py",line 469,in __init__
    data = list(data)
  File "/Users/owner/sBox/Jamesmk6_3/toolBox/financial_analysis.py",line 12,in getdata
    share_price = float("{0:.2f}".format(company_quote[0]['price']))
KeyError: 0

我已经深入挖掘并找到了 dev pages,但作者所做的与他们的文档显示之间似乎存在复杂性。

解决方法

API 有时返回 dict,有时返回 list。更简单的方法是始终使用 json_normalize()

提取

显然,插入您的 API 密钥以完成这项工作。我在 24 小时内用完了允许的调用来进一步测试,它在运行时运行良好。一些股票代码为一些 API 调用返回多行。即最终数据集 > 11 行

import requests
import pandas as pd

tickers = {'AAPL','MSFT','GOOG','T','CSCO','INTC','ORCL','AMZN','FB','TSLA','NVDA'}

df = pd.DataFrame()

url = "https://financialmodelingprep.com/api/v3"
apikey="xxx"
payload = {"apikey":apikey}

for stock in tickers:
    print(stock)
    # use params rather than manually build request parameters
    quote = requests.get(f"{url}/quote/{stock}",params=payload)
    bs = requests.get(f"{url}/balance-sheet-statement/{stock}",params={"period":"quarter","limit":1,**payload})
    IS = requests.get(f"{url}/income-statement/{stock}",**payload})
    company_info = requests.get(f"{url}/company/profile/{stock}",params=payload)

    if "Error Message" in quote.json():
        print(f"Error: {quote.text}")
        break

    else:
        # join all the results together using json_normalise() rather than hand coded extration from JSON
        df = pd.concat([df,(pd.json_normalize(quote.json())
         .merge(pd.json_normalize(bs.json()),on="symbol",suffixes=("","_BS"))
         .merge(pd.json_normalize(IS.json()),"_IS"))
         .merge(pd.json_normalize(company_info.json()),"_info"))
                    )])

# df.columns.tolist()
if len(df)>0:
    # the columns the question is interested in
    df.loc[:,["symbol","price","totalDebt","cashAndShortTermInvestments","revenue","profile.ceo"]]



版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。