1.背景故事–信息来源
获取涨跌停股票池相关信息,内容包含:
- 股票代码
- 名称
- 涨跌幅
- 最新价
- 成交额
- 换手率
- 连板数
【JS返回的数据格式】
【涨停股】http://quote.eastmoney.com/ztb/detail#type=ztgc
【跌停股】http://quote.eastmoney.com/ztb/detail#type=dtgc
2.准备工作—获取校验码
访问如上连接地址,在js中找到push2ex,最后一个js的调用的get请求,就是获取股票池信息的。
复制你浏览器上的ut对应的校验码(后续需要使用)
3.代码实现
import requests, json
# flag传入你需要的涨跌停的标识,涨停股:top 跌停股:bottom
# number是每页显示多少条数据,对应的就是你想获取多少条涨跌停的股票
def get_top_or_bottom_stocks_list(flag, number):
if flag == "top":
url = "http://push2ex.eastmoney.com/getTopicZTPool"
sort_type = "fbt:asc"
elif flag == "bottom":
url = "http://push2ex.eastmoney.com/getTopicDTPool"
sort_type = "fund:asc"
# fund :封单资金(涨跌停均可排序,跌停股默认排序),
# fbt: 首次封板时间(只有涨停股才有这参数,涨停股默认排序)
# ut: 这个字段不同客户端的值是一样的,这个值每天都不变动
param = {"ut": "7eea3edcxxxxxxxxxxxxx", # 此处是你上一步骤获取的ut码
"dpt": "wz.ztzt",
"Pageindex": 0,
"pagesize": 20,
"sort": None,
"date": None}
today = time.strftime('%Y%m%d')
param["date"] = today
param["pagesize"] = number
param["sort"] = sort_type
header = {"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7,la;q=0.6",
"Connection": "keep-alive",
"Host": "push2ex.eastmoney.com",
"Referer": "http://quote.eastmoney.com/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"
}
response = requests.get(url=url, params=param, headers=header)
res = response.json()
data_init = res.get("data")
if data_init:
data_list = data_init.get("pool")
result_list = [(dt.get("c"), dt.get("n")) for dt in data_list]
return result_list