继续上次的话题,今天的目的是要抓取给定公司上市一个月内的股价。
貌似我用了比脚笨的办法,不过不管怎么说还是成功了。代码如下:
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()