循 fenguoerbian 的思路找了一下,奇怪日期为啥是21年,发现这个接口是历史数据,最多到21年,找了一下把 SQLid 换成 "COMMON_SSE_SJ_GPSJ_CJGK_MRGK_C" 就可以找最新的数据了。
加上个format_result的函数把list 转换成data.frame, 数据应该跟网页一致了
library(httr2)
url <- "https://query.sse.com.cn/commonQuery.do"
req <- request(url)
resp <- req %>%
req_url_query(
searchDate = "2024-08-01",
sqlId = "COMMON_SSE_SJ_GPSJ_CJGK_MRGK_C",
stockType = "90",
PRODUCT_CODE="01,02,03,11,17",
type="inParams"
) |>
req_headers(
Connection = "keep-alive",
Accept = "*/*",
`Accept-Encoding` = "gzip, deflate, br, zstd",
`Accept-Language` = "en-US,en;q=0.9,zh;q=0.8,zh-CN;q=0.7",
`Content-Type` = "application/x-www-form-urlencoded",
Host = "query.sse.com.cn",
Referer = "https://www.sse.com.cn/",
`User-Agent` = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36") |>
req_perform()
rslt = resp_body_json(resp)
format_result = function(result){
colsdef=data.table::fread(
"
col_cn col_en
挂牌数 LIST_NUM
市价总值(亿元) TOTAL_VALUE
流通市值(亿元) NEGO_VALUE
成交金额(亿元) TRADE_AMT
成交量(亿股/亿份) TRADE_VOL
平均市盈率(倍) AVG_PE_RATE
换手率(%) TOTAL_TO_RATE
流通换手率(%) NEGO_TO_RATE
"
)
cols=colsdef$col_cn
names(cols)= colsdef$col_en
rslt2 = lapply(result,as.data.frame)
names(rslt2) = c("主板A","主板B","科创板","股票回购","股票")
rslt_final = do.call(rbind, rslt2)
rslt_final=rslt_final[c(5,1,2,3,4),colsdef$col_en]
col_old = colnames(rslt_final)
colnames(rslt_final) = cols[col_old]
return(rslt_final)
}
rslt.df = format_result(rslt$result)
## 转置
as.data.frame(t(rslt.df))
#> 股票 主板A 主板B 科创板 股票回购
#> 挂牌数 2308 1691 43 574 0
#> 市价总值(亿元) 446270.24 398292.29 838.92 47139.02 0.0
#> 流通市值(亿元) 416208.8 381507.3 628.19 34073.32 0.0
#> 成交金额(亿元) 2962.75 2572.86 1.26 388.63 1.65
#> 成交量(亿股/亿份) 297.94 282.74 0.44 14.75 0.21
#> 平均市盈率(倍) 12.25 11.58 6.43 31.67 -
#> 换手率(%) 0.6639 0.646 0.15 0.8244 0.0
#> 流通换手率(%) 0.7118 0.6744 0.2003 1.1406 0.0
<sup>Created on 2024-08-07 with reprex v2.1.0</sup>