[英]how to repeat exception handling on python
import pandas_datareader.data as web
import pandas, xlrd
from datetime import datetime, timedelta
import csv
end = datetime.now() # 마지막 날을 오늘로 설정
start = end - timedelta(days=250) # 시작 날을 오늘로 부터 250일 전으로 설정
stock_code = pandas.read_excel('/home/ubuntu/trading/Book1.xlsx', dtype=str, encoding="CP949").as_matrix()
#250일 모멘텀 계산
for st in stock_code:
eventcode = st[0]
try:
df = web.DataReader("krx:%s" % eventcode, "google", start, end) #코스피이면 구글에서 데이터 받음
except:
df = web.DataReader("%s.KQ" % eventcode, "yahoo", start, end) #코스닥이면 야후에서 데이터 받음
try:
df=web.DataReader("%s.KQ" % eventcode, "yahoo", start, end) #코스닥이면 야후에서 데이터 받음
except:
df = web.DataReader("%s.KQ" % eventcode, "yahoo", start, end) # 코스닥이면 야후에서 데이터 받음
close = df['Close']
num = len(close)
sum = 0
for x in range(0, num - 1):
if close[x + 1] / close[x] == 1: #당일 종가가 전날 종가와 같으면 0 추가
sum = sum + 0
elif close[x + 1] / close[x] > 1: #당일 종가가 전날 종가 보다 높으면 1 추가
sum = sum + 1
else: #당일 종가가 전날 종가가 낮으면 -1.
sum = sum - 1
print(sum) #최종값
Dear all. 亲爱的大家。 I want to get the stock data from google and yahoo.
我想从Google和Yahoo获取股票数据。 sometimes it can't get the data from yahoo.
有时它无法从Yahoo获取数据。 but after few second try it again, i can get data.
但是几秒钟后再试一次,我可以获得数据。
so I use try and except. 所以我用try和except。 if i can't get data from yahoo.
如果我无法从雅虎获取数据。 i want to try again 3 times.
我想再试3次。 so i put the try and except in except.
所以我试了一下,除了除外。 but it doesn't work.
但这不起作用。
how to i fix it? 我该如何解决?
this is errors 这是错误
pandas_datareader._utils.RemoteDataError: Unable to read URL: https://query1.finance.yahoo.com/v7/finance/download/003100.KQ?period1=1483019682&period2=1504655999&crumb=Vxp9S4A%5Cu002FjwQ&interval=1d&events=history
You can go with sleep logic and you can limit the number of retry as well, 您可以采用睡眠逻辑,也可以限制重试次数,
df = None df =无
while df is None: 而df为None时:
try:
df = web.DataReader("krx:%s" % eventcode, "google", start, end)
except:
sleep(1000)
df = None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.