print("数据来源:东方财富网-行情中心-沪深京A股")
import re,requests
import pandas as pd
url = "http://94.push2.eastmoney.com/api/qt/clist/get?cb=jQuery1124040399874179311124_1685159655748&pn=1&pz=6000&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&wbp2u=|0|0|0|web&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1685159655749"
print("\n1:正在爬取数据...")
data = requests.get(url).text
p = re.compile(r'(\"f\d*\":.+?)[,}]')
result = p.findall(data)
print("\n2:数据爬取成功!!!")
stock = {'代码':[],'名称':[],'最新价':[],'涨跌幅%':[],'涨跌额':[],'成交量(手)':[],'成交额':[],'振幅%':[],'最高':[],'最低':[],'今开':[],'昨收':[],'量比':[],'换手率%':[],'市盈率':[],'市净率':[]}
print("\n3:数据正在导出...")
for i in range(1,len(result),31):
stock['代码'].append(result[i+10][6:])
stock['名称'].append(result[i+12][6:])
stock['最新价'].append(result[i][5:])
stock['涨跌幅%'].append(result[i+1][5:])
stock['涨跌额'].append(result[i+2][5:])
stock['成交量(手)'].append(result[i+3][5:])
stock['成交额'].append(result[i+4][5:])
stock['振幅%'].append(result[i+5][5:])
stock['最高'].append(result[i+13][6:])
stock['最低'].append(result[i+14][6:])
stock['今开'].append(result[i+15][6:])
stock['昨收'].append(result[i+16][6:])
stock['量比'].append(result[i+8][6:])
stock['换手率%'].append(result[i+6][5:])
stock['市盈率'].append(result[i+7][5:])
stock['市净率'].append(result[i+20][6:])
df = pd.DataFrame(stock)
df.to_excel(r'示例.xlsx',index=False)
print("\n4:数据导出成功!!!")
input()