繼續(xù)上次的話題遍坟,今天的目的是要抓取給定公司上市一個月內的股價衔统。
貌似我用了比腳笨的辦法激挪,不過不管怎么說還是成功了豆同。代碼如下:
out = open('C:/.../fetchpriceall.txt','w') from matplotlib.finance import quotes_historical_yahoo from matplotlib.dates import YearLocator, MonthLocator, DateFormatter import datetime from datetime import date daysFmt = DateFormatter('%m-%d-%Y') set = [ ('KMI', '2011', '02', '11', '2011', '03', '11'), ...... ...... ('MWRX', '2013', '06', '27', '2013', '07', '27'), ] ticker=[q[0] for q in set] year1=[q[1] for q in set] year1=[int(i) for i in year1] month1=[q[2] for q in set] month1=[int(i) for i in month1] day1=[q[3] for q in set] day1=[int(i) for i in day1] year2=[q[4] for q in set] year2=[int(i) for i in year2] month2=[q[5] for q in set] month2=[int(i) for i in month2] day2=[q[6] for q in set] day2=[int(i) for i in day2] for j in range(len(ticker)): date1 = datetime.date( year1[j], month1[j], day1[j]) date2 = datetime.date( year2[j], month2[j], day2[j]) print(ticker[j]) try: quotes = quotes_historical_yahoo(ticker[j], date1, date2) except: continue datest = [q[0] for q in quotes] closes = [q[2] for q in quotes] closer = [round(float(i),2) for i in closes] datest = [int(i) for i in datest] dates = [date.fromordinal(i) for i in datest] for i in range(len(datest)): print(ticker[j],',',dates[i],',',closer[i], file=out) out.close()