# -*- coding: utf-8 -*-


import urllib.request
import re
##def downback(a,b,c):
##    ''''
##    a:已经下载的数据块
##    b:数据块的大小
##    c:远程文件的大小
##   '''
##    per = 100.0 * a * b / c
##    if per > 100 :
##        per = 100
##    print('%.2f%%' % per)
stock_CodeUrl = 'http://quote.eastmoney.com/stocklist.html'
#获取股票代码列表
def urlTolist(url):
    allCodeList = []
    html = urllib.request.urlopen(url).read()
    html = html.decode('gbk')
    s = r'<li><a target="_blank" href="http://quote.eastmoney.com/\S\S(.*?).html">'
    pat = re.compile(s)
    code = pat.findall(html)
    for item in code:
        if item[0]=='6' or item[0]=='3' or item[0]=='0':
            allCodeList.append(item)
    return allCodeList
    

start = '20180101'
end='20180331'
#start = '20171230'
#end='2018411'


allCodelist = urlTolist(stock_CodeUrl)
for code in allCodelist:
    print('正在获取%s股票数据...'%code)
    if code[0]=='0':
        url = 'http://quotes.money.163.com/service/chddata.html?code=0'+code+\
        '&start='+start+'&end='+end+'&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP'
    else:
        url = 'http://quotes.money.163.com/service/chddata.html?code=1'+code+\
        '&start='+start+'&end='+end+'&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP'
    urllib.request.urlretrieve(url,'E:\\work\\Tensorflow_Getting_Started_and_Practices\\code\\gp\\'+code+'_'+end+'.csv')#可以加一个参数dowmback显示下载进度
    

#test
#code='600000'
#url = 'http://quotes.money.163.com/service/chddata.html?code=0'+code+\
#        '&start='+start+'&end='+end+'&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP'
#urllib.request.urlretrieve(url,'E:\\work\\Tensorflow_Getting_Started_and_Practices\\code\\'+code+'_'+end+'.csv')#可以加一个参数dowmback显示下载进度
    