1. 程式人生 > >中金所金融期貨日統計資料爬取

中金所金融期貨日統計資料爬取

基本思路

構造URL下載每個交易日統計的附件csv檔案,提取csv檔案中的次月交割的股指期貨統計資料

程式碼塊

from io import StringIO  
import csv  
import urllib.request
import re

def get_csv(month,day):
    """構造URL下載日統計資料檔案"""
    try:
        url='http://www.cffex.com.cn/sj/hqsj/rtj/2018'+month+'/'+day+'/2018'+month+day+'_1.csv' 
        data=urllib.request.urlopen(url).read().decode('ascii', 'ignore')  
        dataFile = StringIO(data)  
        csvReader = csv.reader(dataFile)  
        print(url)
        with open('data_cffex_IH.csv','a',encoding='utf-8',newline='') as csvfile:
        fieldnames=['time','code','open','highest','lowest','volume','inventory','close','settlement','zhangdie1','zhangdie2','implied volatility','delta']
        writer=csv.DictWriter(csvfile,fieldnames=fieldnames)
        time='2018'+month+day
        for row in csvReader:

            if row[0]=='IH18'+str(int(month)+1).zfill(2)+'                        ':  #row[0]=='IH18'+str(int(month)+1).zfill(2)+'                        '
                row.insert(12,time)
                print(row[12]+row[0]+'!')

                writer.writerow({'code':row[0],'open':row[1],'highest':row[2],'lowest':row[3],'volume':row[4],'inventory':row[5],'close':row[6],'settlement':row[7],'zhangdie1':row[8],'zhangdie2':row[9],'implied volatility':row[10],'delta':row[11],'time':row[12]})
    except:
        print("It is not a trading day! ")


if __name__=='__main__':


    for month in range(1,2):  #改變range引數可以獲取不同月份的日統計檔案
        month=str(month)
        month=month.zfill(2)
        for day in range(1,32):  #提取31日的日統計檔案
            day=str(day)
            day=day.zfill(2)
            get_csv(month,day)