我正在尝试检索数据到csv。
而且我正在得到很多错误,我已经安装了Pandas和Python,有人能帮忙吗?
import requests
import pandas as pd
import arrow
import datetime
def get_quote_data(symbol='SBIN.NS', data_range='1d', data_interval='1m'):
res = requests.get('https://query1.finance.yahoo.com/v8/finance/chart/{symbol}?range={data_range}&interval={data_interval}'.format(**locals()))
data = res.json()
body = data['chart']['result'][0]
dt = datetime.datetime
dt = pd.Series(map(lambda x: arrow.get(x).to('Asia/Calcutta').datetime.replace(tzinfo=None), body['timestamp']), name='Datetime')
df = pd.DataFrame(body['indicators']['quote'][0], index=dt)
dg = pd.DataFrame(body['timestamp'])
return df.loc[:, ('open', 'high', 'low', 'close', 'volume')]
data = get_quote_data('SBIN.NS', '5d', '1m')
data.dropna(inplace=True) #removing NaN rows
print(data)
data.to_csv('output.csv')
这些是我得到的错误:
>>> import requests
>>> import pandas as pd
>>> import arrow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'arrow'
>>> import datetime
>>>
>>> def get_quote_data(symbol='SBIN.NS', data_range='1d', data_interval='1m'):
... res = requests.get('https://query1.finance.yahoo.com/v8/finance/chart/{symbol}?range={data_range}&interval={data_interval}'.format(**locals()))
File "<stdin>", line 2
res = requests.get('https://query1.finance.yahoo.com/v8/finance/chart/{symbol}?range={data_range}&interval={data_interval}'.format(**locals()))
^
IndentationError: expected an indented block after function definition on line 1
>>> data = res.json()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'res' is not defined
>>> body = data['chart']['result'][0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'data' is not defined
>>> dt = datetime.datetime
>>> dt = pd.Series(map(lambda x: arrow.get(x).to('Asia/Calcutta').datetime.replace(tzinfo=None), body['timestamp']), name='Datetime')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'body' is not defined
>>> df = pd.DataFrame(body['indicators']['quote'][0], index=dt)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'body' is not defined
>>> dg = pd.DataFrame(body['timestamp'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'body' is not defined
>>>
>>> return df.loc[:, ('open', 'high', 'low', 'close', 'volume')]
File "<stdin>", line 1
SyntaxError: 'return' outside function
>>> data = get_quote_data('SBIN.NS', '5d', '1m')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'get_quote_data' is not defined
>>> data.dropna(inplace=True) #removing NaN rows
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'data' is not defined
>>> print(data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'data' is not defined
>>> data.to_csv('output.csv')
我不是很熟悉编码,但我需要检索这些数据来运行回归。有什么变通办法可以让它工作吗?谢谢
1条答案
按热度按时间m1m5dgzv1#
您必须缩进函数下面的代码,并将
headers
设置为requests
的参数,以避免403错误。更新
您可以使用
yfinance
包从Yahoo下载数据: