[置顶] Python多线程获取上证50成分股交易数据

1. 上证50成分股

上证50指数依据样本稳定性和动态跟踪相结合的原则,每半年调整一次成份股,调整时间与上证180指数一致。特殊情况时也可能对样本进行临时调整。
每次调整的比例一般情况不超过10%。样本调整设置缓冲区,排名在40名之前的新样本优先进入,排名在60名之前的老样本优先保留。查看详情,请点击这里

2016-02-25发布

浦发银行 (600000)   包钢股份 (600010)   华夏银行 (600015)
民生银行 (600016)   上港集团 (600018)   中国石化 (600028)
中信证券 (600030)   招商银行 (600036)   保利地产 (600048)
中国联通 (600050)   上汽集团 (600104)   国金证券 (600109)
北方稀土 (600111)   中国船舶 (600150)   康美药业 (600518)
贵州茅台 (600519)   海螺水泥 (600585)   东方明珠 (600637)
国电电力 (600795)   海通证券 (600837)   伊利股份 (600887)
中航动力 (600893)   东方证券 (600958)   招商证券 (600999)
大秦铁路 (601006)   中国神华 (601088)   兴业银行 (601166)
北京银行 (601169)   中国铁建 (601186)   国泰君安 (601211)
农业银行 (601288)   中国平安 (601318)   交通银行 (601328)
新华保险 (601336)   中国中铁 (601390)   工商银行 (601398)
中国太保 (601601)   中国人寿 (601628)   中国建筑 (601668)
中国电建 (601669)   华泰证券 (601688)   中国中车 (601766)
中国交建 (601800)   光大银行 (601818)   中国石油 (601857)
方正证券 (601901)   中国核电 (601985)   中国银行 (601988)
中国重工 (601989)   中信银行 (601998)   

对以上数据处理后保存到一个SH50.list文件中,参见源代码。

600000.SS 浦发银行
600010.SS 包钢股份
600015.SS 华夏银行
600016.SS 民生银行
600018.SS 上港集团
600028.SS 中国石化
600030.SS 中信证券
600036.SS 招商银行
600048.SS 保利地产
600050.SS 中国联通

2. 读取文件

def read_stocks(stock_file):
    print 'read_stocks...'
    ls_stock_name = []
    f = open(stock_file, 'r')
    for line in f.readlines():
        if line.strip():
            ls_stock_name.append(line.strip().split(' ')[0])
    f.close()

    return ls_stock_name

3. 从Yahoo获取股票数据

Python获取Yahoo股票数据

def get_yahoo_data(stock):
    start = time.time()
    res = True
    _now = datetime.datetime.now()
    try:
        if stock[0] == '$':
            stock = '^' + stock[1:]
        print 'get %s ...' % stock
        f = open(data_path + '/' + stock + ".csv", 'w')
        params = urllib.urlencode(
            {'a': 1, 'b': 1, 'c': 2004, 'd': _now.day, 'e': _now.month, 'f': _now.year, 's': stock})
        url = "http://ichart.finance.yahoo.com/table.csv?%s" % params
        time.sleep(0.5)
        url_get = urllib2.urlopen(url)

        data = url_get.readline()
        while(len(data) > 0):
            # print data
            f.write(data)
            data = url_get.readline()

        f.close()
        print "Fetch [%s] done, cost : %s." % (stock,  (time.time() - start))

    except urllib2.HTTPError:
        res = False
        miss_stock.append(stock)
        print "Unable to fetch data for stock: {0} at {1}".format(stock, url)
    except urllib2.URLError:
        res = False
        miss_stock.append(stock)
        print "URL Error for stock: {0} at {1}".format(stock, url)
    except SocketError as e:
        res = False
        miss_stock.append(stock)
        print "Socket Error for stock: {0} at {1}".format(stock, url)
    except :
        res = False
        miss_stock.append(stock)
        print "Unknown Error for stock: {0} at {1}".format(stock, url)

    return res

4. 多线程模块

def get_yahoo_datas(ls_stock_name):
    print 'get_yahoo_data...'
    if len(ls_stock_name) <= 0:
        return
    print data_path
    print ls_stock_name
    if not (os.access(data_path, os.F_OK)):
        os.makedirs(data_path)

# pool = mythreadpool.ThreadPool(8)
# for stock in ls_stock_name:
# pool.add_job(get_yahoo_data, stock)
# pool.wait()

    pool = threadpool.ThreadPool(10)
    reqs = threadpool.makeRequests(get_yahoo_data, ls_stock_name)
    [pool.putRequest(req) for req in reqs]
    pool.wait()

    print "All done. Got {0} stocks. Could not get {1}".format(len(ls_stock_name) - len(miss_stock), len(miss_stock))
    return

5. 源代码

http://download.csdn.net/detail/xiyanlgu/9443331

6. 参考文献

[1] 上证50指数成分股列表 http://www.sse.com.cn/market/sseindex/indexlist/s/i000016/const_list.shtml

你可能感兴趣的:(多线程,python,Yahoo,股票)