我试图运行这段代码:
import pandas as pd
df = pd.read_csv(r"C:UsersChaitanyaDesktopFinsearchAAPL.csv")
df = df.sort_values(by="Date")
df = df.dropna()
# calculate returns
df = df.assign(close_day_before=df.Close.shift(1))
df['returns'] = ((df.Close - df.close_day_before)/df.close_day_before)
# get options data from yahoo finance // in this case, exercise data is july 24th
r = pd.read_html('http://finance.yahoo.com/quote/AAPL/options?date=1655424000&p=AAPL')[0]
它显示错误:
Traceback (most recent call last):
File "c:UsersChaitanyaDesktopFinsearchfinserach.py", line 10, in <module>
r = pd.read_html('http://finance.yahoo.com/quote/AAPL/options?date=1655424000&p=AAPL')[0]
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39libsite-packagespandasutil_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39libsite-packagespandasiohtml.py", line 1098, in read_html
return _parse(
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39libsite-packagespandasiohtml.py", line 906, in _parse
tables = p.parse_tables()
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39libsite-packagespandasiohtml.py", line 222, in parse_tables
tables = self._parse_tables(self._build_doc(), self.match, self.attrs)
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39libsite-packagespandasiohtml.py", line 745, in _build_doc
raise e
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39libsite-packagespandasiohtml.py", line 726, in _build_doc
with urlopen(self.io) as f:
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39libsite-packagespandasiocommon.py", line 211, in urlopen
return urllib.request.urlopen(*args, **kwargs)
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39liburllibrequest.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39liburllibrequest.py", line 523, in open
response = meth(req, response)
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39liburllibrequest.py", line 632, in http_response
response = self.parent.error(
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39liburllibrequest.py", line 555, in error
result = self._call_chain(*args)
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39liburllibrequest.py", line 494, in _call_chain
result = func(*args)
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39liburllibrequest.py", line 747, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39liburllibrequest.py", line 523, in open
response = meth(req, response)
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39liburllibrequest.py", line 632, in http_response
response = self.parent.error(
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39liburllibrequest.py", line 561, in error
return self._call_chain(*args)
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39liburllibrequest.py", line 494, in _call_chain
result = func(*args)
File "C:UsersChaitanyaAppDataLocalProgramsPythonPython39liburllibrequest.py", line 641, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found``
您发布的堆栈跟踪似乎表明错误来自您尚未在问题中发布的代码:
Traceback (most recent call last):
File "c:UsersChaitanyaDesktopFinsearchfinserach.py",
line 10, in **r = pd.read_html('http://finance.yahoo.com/quote/AAPL/options?date=1655424000&p=AAPL')**[0] File
具体来说,r = pd_read_html('...)部分。