--- title: 通过股票代码查股票信息 tags: - Python - re - 爬虫 - 股票 id: '139' categories: - - Python练习 date: 2020-06-03 16:22:03 --- ``` import requests, re bg = r'
' ed = r'

最新流通股:

' r = re.compile(r'<(.+) ?(.*?)>(.*)') r2 = re.compile(r'<(.+) ?(.*?)>(.*)(.*)') 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')) ```