使用panda从csv文件读取数据时出错


import yfinance as yahooFinance
import time
import datetime
import pandas as pd
ticker = 'TSLA'
period1 = int(time.mktime(datetime.datetime(2022, 12, 1, 23, 59).timetuple()))  # year,month,date
period2 = int(time.mktime(datetime.datetime(2022, 12, 31, 23, 59).timetuple()))  # year,month,date
interval = '1wk'  # time interval
query_string = f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={period1}&period2={period2}&interval={interval}&events=history&includeAdjustedClose=true'
df = pd.read_csv(f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={period1}&period2={period2}&interval={interval}&events=history&includeAdjustedClose=true')
print(df)

以上是我的代码当我运行它时,会出现以下错误

File "<stdin>", line 1, in <module>
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39libsite-packagespandasutil_decorators.py", line 311, in wrapper      
return func(*args, **kwargs)
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39libsite-packagespandasioparsersreaders.py", line 680, in read_csv   
return _read(filepath_or_buffer, kwds)
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39libsite-packagespandasioparsersreaders.py", line 575, in _read      
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39libsite-packagespandasioparsersreaders.py", line 933, in __init__   
self._engine = self._make_engine(f, self.engine)
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39libsite-packagespandasioparsersreaders.py", line 1217, in _make_engine
self.handles = get_handle(  # type: ignore[call-overload]
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39libsite-packagespandasiocommon.py", line 670, in get_handle
ioargs = _get_filepath_or_buffer(
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39libsite-packagespandasiocommon.py", line 339, in _get_filepath_or_buffer
with urlopen(req_info) as req:
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39libsite-packagespandasiocommon.py", line 239, in urlopen
return urllib.request.urlopen(*args, **kwargs)
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39liburllibrequest.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39liburllibrequest.py", line 523, in open
response = meth(req, response)
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39liburllibrequest.py", line 632, in http_response
response = self.parent.error(
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39liburllibrequest.py", line 561, in error
return self._call_chain(*args)
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39liburllibrequest.py", line 494, in _call_chain
result = func(*args)
File "C:UsersBRBCOAppDataLocalProgramsPythonPython39liburllibrequest.py", line 641, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

我做错了什么?

period1period2都是无效的,因为它们在未来。调整它们,例如用datetime.date(2021,4,5)datetime.date.today()来获得当前日期,即可能的最晚日期。

import time
import datetime
import pandas as pd
ticker = 'TSLA'
period1 = int(time.mktime(datetime.date(2021,4,5).timetuple()))
period2 = int(time.mktime(datetime.date.today().timetuple())) 
interval = '1wk'
query_string = f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={period1}&period2={period2}&interval={interval}&events=history&includeAdjustedClose=true'
df = pd.read_csv(query_string)

最新更新