通过股票代码查股票信息

发布于 2020-06-03  594 次阅读


import requests, re
bg = r'<div class="com_overview blue_d">'
ed = r'<p><b>最新流通股:</b></p>'
r = re.compile(r'<(.+) ?(.*?)>(.*)</\1>')
r2 = re.compile(r'<(.+) ?(.*?)>(.*)</\1>(.*)')
r3 = re.compile(r'((?:(?:SH)?[69]|(?:SZ)?[02]|(?:HK)?)0\d{4})$',flags=re.IGNORECASE)
def mcOne(code):
    assert r3.match(code), '股票代码格式错误'
    res = requests.get(f'http://finance.sina.com.cn/realstock/company/{code}/nc.shtml')
    res.encoding = 'gb2312'
    html = res.text
    #html = res.content.decode('gbk')
    s = html.find(bg)
    e = html.find(ed, s)
    mc = r.findall(html[s:e].replace('\u3000',''))
    st = {}
    st[r.findall(mc[0][2])[0][2]] = mc[1][2]
    st[r.findall(mc[2][2])[0][2]] = mc[3][1][7:-1]
    for i in range(4,len(mc)):
        temp = r2.findall(mc[i][2])[0]
        st[temp[2]] = temp[3]
    return st
print (mcOne('sh600000'))

医学生