Stock data acquisition with interface

the urllib.request Import 
Import Re 

codeUrl = 'https://hq.gucheng.com/gpdmylb.html' 

# Get ticker 
DEF codeTolist (): 
    CodeList = [] 
    HTML = the urllib.request.urlopen (codeUrl) .read () 
    = html.decode HTML ( 'UTF-. 8') 
    S = '<a href="https://hq.gucheng.com/(SZ|SH)(.*?)/">' 
    P = the re.compile ( S) 
    data = p.findall (HTML) 
    for code in data: 
        codeList.append (code [. 1]) 
    return CodeList 

allCodeList codeTolist = () 

# download stock data 
DEF allDown (): 
    T = len (allCodeList) 
    for code in allCodeList : 
        Print (F 'is {code} stock data acquisition') 
        IF code [0] == '. 6':
            Url = 'http://quotes.money.163.com/service/chddata.html?code=0'+code+\
                '&start=19981218&end=20191213&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=19981218&end=20191213&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP'
        urllib.request.urlretrieve(Url,'d:\\gupiao\\'+code+'.csv')
        t = t -1
        if t == 0:
            print('所有股票下载完毕!')
allDown()

  

Guess you like

Origin www.cnblogs.com/wumac/p/12040838.html