我在下面粘贴的代码出错。我试图在论坛上寻找类似的问题,老实说,我确实发现了一些相同的问题。但是,其中一些还没有解决方案,而那些拥有解决方案的解决方案不适用于我的情况(至少我无法使其工作)。粘贴代码片段,如下所示:
# coding: utf-8
# In[14]:
import csv
import datetime
import re
import codecs
import requests
import pandas as pd
#import cufflinks as cf
from plotly.offline import init_notebook_mode, iplot
import numpy as np
def get_google_finance_intraday(ticker, period=60, days=20, exchange='NSE'):
# build url
url = 'https://finance.google.com/finance/getprices' + '?p={days}d&f=d,o,h,l,c,v&q={ticker}&i={period}&x={exchange}'.format(ticker=ticker,
period=period,
days=days,
exchange=exchange)
page = requests.get(url)
reader = csv.reader(codecs.iterdecode(page.content.splitlines(), "utf-8"))
columns = ['Open', 'High', 'Low', 'Close', 'Volume']
rows = []
times = []
for row in reader:
if re.match('^[a\d]', row[0]):
if row[0].startswith('a'):
start = datetime.datetime.fromtimestamp(int(row[0][1:]))
times.append(start)
else:
times.append(start+datetime.timedelta(seconds=period*int(row[0])))
rows.append(map(float, row[1:]))
if len(rows):
return pd.DataFrame(rows, index=pd.DatetimeIndex(times, name='Date'), columns=columns)
else:
return pd.DataFrame(rows, index=pd.DatetimeIndex(times, name='Date'))
i = 0
with open('1.0.Ticker_names -MasterCopy.csv') as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
symbol_name = row
symbol = "".join(map(str, symbol_name))
# input data
ticker = symbol
period = 60
days = 200
exchange = 'NSE'
df = get_google_finance_intraday(ticker, period=period, days=days)
ohlc_dict = {
'Open':'first',
'High':'max',
'Low':'min',
'Close': 'last',
'Volume': 'sum'
}
df = df.resample('60MIN',base=15).apply(ohlc_dict).dropna(how='any')
df['Name'] = ticker
df.to_csv(ticker+'.csv')
csv文件-1.0.Ticker_names -MasterCopy.csv在A列中仅具有股票名称,名称以“ A1”单元格开头。
我收到以下错误:
Traceback (most recent call last):
File "C:\Users\Lenovo\Downloads\0-Copy1.0.Final_Intraday Stock data_v0.1_22June2018 (2).py", line 80, in <module>
df = get_google_finance_intraday(ticker, period=period, days=days)
File "C:\Users\Lenovo\Downloads\0-Copy1.0.Final_Intraday Stock data_v0.1_22June2018 (2).py", line 54, in get_google_finance_intraday
for row in reader:
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\codecs.py", line 1041, in iterdecode
output = decoder.decode(input)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 6952: invalid start byte