import requests import datetime # 获取当前日期对象 from ExcelDemo import Demo now = datetime.datetime.now() year = now.year # 年份 month = str(now.month) # 月份 # 因为发请求,如果是月份是1位,需要前面加0 if len(month)==1: month = f'0{month}' #12 day = now.day headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', } # 循环的次数 = 本月1号到今天的日期天数 for i in range(1,day+1): # 31 30 # 202411 20240101 if len(str(i)) == 1: i = f'0{i}' date = f'{year}{month}{i}' url = f'https://push2ex.eastmoney.com/getTopicZTPool?ut=7eea3edcaed734bea9cbfc24409ed989&dpt=wz.ztzt&Pageindex=0&pagesize=100&sort=fbt%3Aasc&date={date}&_=1704547600462' # 发起请求 获取响应json数据 res = requests.get(url,headers=headers).json() # 取出pool 如果没有数据 pool:[] pool = res['data']['pool'] for i in pool: # 取出数据值 # 代码 code = i['c'] # 名称 name = i['n'] # 涨跌幅 # zdp = round(i['zdp'],2) + '%' zdp = f"{round(i['zdp'], 2)}%" # 价格 price = i['p'] / 1000 # 成交额 amount = f"{round(i['amount'] / 100000000, 2)}亿" # 首次封板时间 fbt = str(i['fbt']) fbt = f'{fbt[:-4]}:{fbt[-4:-2]}:{fbt[-2:]}' # 92500 data = { date: [code, name, zdp, price, amount, fbt] } Demo.SaveExcels(data, '东方财富涨停板', '当月涨停板数据', ('代码', '名称', '涨跌幅', '价格', '成交额', '首次封板时间'))
python 爬A股数据的代码
于 2024-06-29 11:52:41 首次发布