如何获得 Python 中的股票市场价格。可靠,每次都有效

How to get stock market prices in Python. Reliable that works every time

我希望将股票市场数据导入数据框。我试过 web.DataReader 和 Yahoo Finance,结果如下。 我在网上寻找从互联网获取股票数据的“可重复”解决方案。 我已经尝试过 yahoo finance 和 web.DataReader 解决方案。这些结果发布在下面。

我也试过 Quandl,但它没有我要找的所有代码....

df_IJR = yf.download("IJR", start=start_data, end=end_data).reset_index()

df_IJR = yf.download("IJR", start=start_data, end=end_data).reset_index()
Exception in thread Thread-22:
Traceback (most recent call last):
  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\site-packages\multitasking\__init__.py", line 102, in _run_via_pool
    return callee(*args, **kwargs)
  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\site-packages\yfinance\multi.py", line 167, in _download_one_threaded
    data = _download_one(ticker, start, end, auto_adjust, back_adjust,
  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\site-packages\yfinance\multi.py", line 179, in _download_one
    return Ticker(ticker).history(period=period, interval=interval,
  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\site-packages\yfinance\base.py", line 157, in history
    data = data.json()
  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\site-packages\requests\models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

这里是 web.DataReader 结果。

df_google = web.DataReader("GOOGL", 'yahoo', start_data, end_data)['Close'].reset_index()
Traceback (most recent call last):

  File "<ipython-input-35-cd9c40cdca1d>", line 1, in <module>
    df_google = web.DataReader("GOOGL", 'yahoo', start_data, end_data)['Close'].reset_index()

  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\site-packages\pandas\util\_decorators.py", line 199, in wrapper
    return func(*args, **kwargs)

  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\site-packages\pandas_datareader\data.py", line 376, in DataReader
    return YahooDailyReader(

  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\site-packages\pandas_datareader\base.py", line 253, in read
    df = self._read_one_data(self.url, params=self._get_params(self.symbols))

  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\site-packages\pandas_datareader\yahoo\daily.py", line 153, in _read_one_data
    resp = self._get_response(url, params=params)

  File "D:\HPDocs\MyProgramFiles\Anaconda3\lib\site-packages\pandas_datareader\base.py", line 181, in _get_response
    raise RemoteDataError(msg)

RemoteDataError: Unable to read URL: https://finance.yahoo.com/quote/GOOGL/history?period1=1577876400&period2=1625911199&interval=1d&frequency=1d&filter=history
Response Text:
b'<!DOCTYPE html>\n  <html lang="en-us"><head>\n  <meta http-equiv="content-type" content="text/html; charset=UTF-8">\n      <meta charset="utf-8">\n      <title>Yahoo</title>\n      <meta name="viewport" content="width=device-width,initial-scale=1,minimal-ui">\n      <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n      <style>\n  html {\n      height: 100%;\n  }\n  body {\n      background: #fafafc url(https://s.yimg.com/nn/img/sad-panda-201402200631.png) 50% 50%;\n      background-size: cover;\n      height: 100%;\n      text-align: center;\n      font: 300 18px "helvetica neue", helvetica, verdana, tahoma, arial, sans-serif;\n  }\n  table {\n      height: 100%;\n      width: 100%;\n      table-layout: fixed;\n      border-collapse: collapse;\n      border-spacing: 0;\n      border: none;\n  }\n  h1 {\n      font-size: 42px;\n      font-weight: 400;\n      color: #400090;\n  }\n  p {\n      color: #1A1A1A;\n  }\n  #message-1 {\n      font-weight: bold;\n      margin: 0;\n  }\n  #message-2 {\n      display: inline-block;\n      *display: inline;\n      zoom: 1;\n      max-width: 17em;\n      _width: 17em;\n  }\n      </style>\n  <script>\n    document.write(\'<img src="//geo.yahoo.com/b?s=1197757129&t=\'+new Date().getTime()+\'&src=aws&err_url=\'+encodeURIComponent(document.URL)+\'&err=%<pssc>&test=\'+encodeURIComponent(\'%<{Bucket}cqh[:200]>\')+\'" width="0px" height="0px"/>\');var beacon = new Image();beacon.src="//bcn.fp.yahoo.com/p?s=1197757129&t="+new Date().getTime()+"&src=aws&err_url="+encodeURIComponent(document.URL)+"&err=%<pssc>&test="+encodeURIComponent(\'%<{Bucket}cqh[:200]>\');\n  </script>\n  </head>\n  <body>\n  <!-- status code : 404 -->\n  <!-- Not Found on Server -->\n  <table>\n  <tbody><tr>\n      <td>\n      <img src="https://s.yimg.com/rz/p/yahoo_frontpage_en-US_s_f_p_205x58_frontpage.png" alt="Yahoo Logo">\n      <h1 style="margin-top:20px;">Will be right back...</h1>\n      <p id="message-1">Thank you for your patience.</p>\n      <p id="message-2">Our engineers are working quickly to resolve the issue.</p>\n      </td>\n  </tr>\n  </tbody></table>\n  </body></html>'

试试这个:

import datetime as dt
import pandas as pd
import yfinance as yf
from pandas_datareader import data as pdr

tickers = ["AAPL","GOOG","AMZN"]
yf.download(tickers,  start = "2021-02-01" , end = "2021-02-04")

我还建议查看 financialmodelingprep,它们非常可靠并且有免费版本。