1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 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 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'))
|