stock-tracker

This commit is contained in:
C菌
2026-07-04 00:17:11 +08:00
commit 6087341a48
6463 changed files with 1929869 additions and 0 deletions
@@ -0,0 +1,6 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2019/9/30 13:58
Desc:
"""
@@ -0,0 +1,195 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2021/5/27 20:19
Desc: 指数配置文件
"""
# weibo-user-agent
index_weibo_headers = {
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) "
"AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
"Referer": "http://data.weibo.com/index/newindex",
"Accept": "application/json",
"Origin": "https://data.weibo.com",
}
# sw-cons
sw_cons_headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Connection": "keep-alive",
"Content-Length": "34",
"Content-Type": "text/plain; charset=UTF-8",
# "Cookie": "ASP.NET_SessionId=i55eaz55142xdxfx0bkqp145",
"Host": "www.swsindex.com",
"Origin": "http://www.swsindex.com",
"Referer": "http://www.swsindex.com/idx0210.aspx?swindexcode=801010",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/78.0.3904.108 Safari/537.36",
"X-AjaxPro-Method": "ReturnContent",
}
# sw-url
sw_url = "http://www.swsindex.com/handler.aspx"
# sw-payload
sw_payload = {
"tablename": "swzs",
"key": "L1",
"p": "1",
"where": "L1 in('801010','801020','801030','801040','801050','801080','801110','801120','801130',"
"'801140','801150','801160','801170','801180','801200','801210','801230','801710','801720',"
"'801730','801740','801750','801760','801770','801780','801790','801880','801890','801950',"
"'801960','801970','801980')",
"orderby": "",
"fieldlist": "L1,L2,L3,L4,L5,L6,L7,L8,L11",
"pagecount": "28",
"timed": "",
}
# sw-headers
sw_headers = {
"Accept": "application/json, text/javascript, */*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
"Content-Type": "application/x-www-form-urlencoded",
"DNT": "1",
"Host": "www.swsindex.com",
"Origin": "http://www.swsindex.com",
"Pragma": "no-cache",
"Referer": "http://www.swsindex.com/idx0120.aspx?columnid=8832",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/74.0.3729.169 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
}
# zh-sina-a
zh_sina_index_stock_url = (
"http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/"
"Market_Center.getHQNodeDataSimple"
)
zh_sina_index_stock_payload = {
"page": "1",
"num": "80",
"sort": "symbol",
"asc": "1",
"node": "hs_s",
"_s_r_a": "page",
}
zh_sina_index_stock_count_url = (
"http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/"
"Market_Center.getHQNodeStockCountSimple?node=hs_s"
)
zh_sina_index_stock_hist_url = (
"https://finance.sina.com.cn/realstock/company/{}/hisdata/klc_kl.js"
)
# investing
short_headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/61.0.3163.91 Safari/537.36"
}
long_headers = {
"accept": "text/plain, */*; q=0.01",
# 'accept-encoding': 'gzip, deflate, br',
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
# 'cache-control': 'no-cache',
"content-length": "267",
"content-type": "application/x-www-form-urlencoded",
"origin": "https://cn.investing.com",
"referer": "https://cn.investing.com/commodities/brent-oil-historical-data",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/61.0.3163.91 Safari/537.36",
"x-requested-with": "XMLHttpRequest",
}
index_global_sina_symbol_map = {
# 欧洲股市
"英国富时100指数": "UKX",
"德国DAX 30种股价指数": "DAX",
"俄罗斯MICEX指数": "INDEXCF",
"法CAC40指数": "CAC",
"瑞士股票指数": "SWI20",
"富时意大利MIB指数": "FTSEMIB",
"荷兰AEX综合指数": "AEX",
"西班牙IBEX指数": "IBEX",
"欧洲Stoxx50指数": "SX5E",
# 美洲股市
"加拿大S&P/TSX综合指数": "GSPTSE",
"墨西哥BOLSA指数": "MXX",
"巴西BOVESPA股票指数": "IBOV",
# 亚洲股市
"中国台湾加权指数": "TWJQ",
"日经225指数": "NKY",
"首尔综合指数": "KOSPI",
"印度尼西亚雅加达综合指数": "JCI",
"印度孟买SENSEX指数": "SENSEX",
# 澳洲股市
"澳大利亚标准普尔200指数": "AS51",
"新西兰NZSE 50指数": "NZ250",
# 非洲股市
"埃及CASE 30指数": "CASE",
}
index_global_em_symbol_map = {
"波罗的海BDI指数": {"code": "BDI", "market": "100"},
"葡萄牙PSI20": {"code": "PSI20", "market": "100"},
"菲律宾马尼拉": {"code": "PSI", "market": "100"},
"泰国SET": {"code": "SET", "market": "100"},
"俄罗斯RTS": {"code": "RTS", "market": "100"},
"巴基斯坦卡拉奇": {"code": "KSE100", "market": "100"},
"越南胡志明": {"code": "VNINDEX", "market": "100"},
"红筹指数": {"code": "HSCCI", "market": "124"},
"印尼雅加达综合": {"code": "JKSE", "market": "100"},
"希腊雅典ASE": {"code": "ASE", "market": "100"},
"墨西哥BOLSA": {"code": "MXX", "market": "100"},
"挪威OSEBX": {"code": "OSEBX", "market": "100"},
"巴西BOVESPA": {"code": "BVSP", "market": "100"},
"波兰WIG": {"code": "WIG", "market": "100"},
"印度孟买SENSEX": {"code": "SENSEX", "market": "100"},
"布拉格指数": {"code": "PX", "market": "100"},
"荷兰AEX": {"code": "AEX", "market": "100"},
"冰岛ICEX": {"code": "ICEXI", "market": "100"},
"斯里兰卡科伦坡": {"code": "CSEALL", "market": "100"},
"富时新加坡海峡时报": {"code": "STI", "market": "100"},
"富时意大利MIB": {"code": "MIB", "market": "100"},
"路透CRB商品指数": {"code": "CRB", "market": "100"},
"比利时BFX": {"code": "BFX", "market": "100"},
"富时AIM全股": {"code": "AXX", "market": "100"},
"新西兰50": {"code": "NZ50", "market": "100"},
"上证指数": {"code": "000001", "market": "1"},
"国企指数": {"code": "HSCEI", "market": "100"},
"沪深300": {"code": "000300", "market": "1"},
"英国富时100": {"code": "FTSE", "market": "100"},
"中小100": {"code": "399005", "market": "0"},
"瑞士SMI": {"code": "SSMI", "market": "100"},
"西班牙IBEX35": {"code": "IBEX", "market": "100"},
"瑞典OMXSPI": {"code": "OMXSPI", "market": "100"},
"爱尔兰综合": {"code": "ISEQ", "market": "100"},
"韩国KOSPI": {"code": "KS11", "market": "100"},
"深证成指": {"code": "399001", "market": "0"},
"韩国KOSPI200": {"code": "KOSPI200", "market": "100"},
"芬兰赫尔辛基": {"code": "HEX", "market": "100"},
"恒生指数": {"code": "HSI", "market": "100"},
"欧洲斯托克50": {"code": "SX5E", "market": "100"},
"美元指数": {"code": "UDI", "market": "100"},
"法国CAC40": {"code": "FCHI", "market": "100"},
"台湾加权": {"code": "TWII", "market": "100"},
"英国富时250": {"code": "MCX", "market": "100"},
"富时马来西亚KLCI": {"code": "KLSE", "market": "100"},
"OMX哥本哈根20": {"code": "OMXC20", "market": "100"},
"道琼斯": {"code": "DJIA", "market": "100"},
"奥地利ATX": {"code": "ATX", "market": "100"},
"加拿大S&P/TSX": {"code": "TSX", "market": "100"},
"德国DAX30": {"code": "GDAXI", "market": "100"},
"创业板指": {"code": "399006", "market": "0"},
"澳大利亚普通股": {"code": "AORD", "market": "100"},
"标普500": {"code": "SPX", "market": "100"},
"澳大利亚标普200": {"code": "AS51", "market": "100"},
"日经225": {"code": "N225", "market": "100"},
"纳斯达克": {"code": "NDX", "market": "100"},
}
@@ -0,0 +1,132 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2021/12/27 15:47
Desc: 中国公路物流运价、运量指数
http://index.0256.cn/expx.htm
"""
import pandas as pd
import requests
def index_price_cflp(symbol: str = "周指数") -> pd.DataFrame:
"""
中国公路物流运价指数
http://index.0256.cn/expx.htm
:param symbol: choice of {"周指数", "月指数", "季度指数", "年度指数"}
:type symbol: str
:return: 中国公路物流运价指数
:rtype: pandas.DataFrame
"""
symbol_map = {
"周指数": "2",
"月指数": "3",
"季度指数": "4",
"年度指数": "5",
}
url = "http://index.0256.cn/expcenter_trend.action"
params = {
"marketId": "1",
"attribute1": "5",
"exponentTypeId": symbol_map[symbol],
"cateId": "2",
"attribute2": "华北",
"city": "",
"startLine": "",
"endLine": "",
}
headers = {
"Origin": "http://index.0256.cn",
"Referer": "http://index.0256.cn/expx.htm",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/90.0.4430.212 Safari/537.36",
}
r = requests.post(url, data=params, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(
[
data_json["chart1"]["xLebal"],
data_json["chart1"]["yLebal"],
data_json["chart2"]["yLebal"],
data_json["chart3"]["yLebal"],
]
).T
temp_df.columns = ["日期", "定基指数", "环比指数", "同比指数"]
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["定基指数"] = pd.to_numeric(temp_df["定基指数"], errors="coerce")
temp_df["环比指数"] = pd.to_numeric(temp_df["环比指数"], errors="coerce")
temp_df["同比指数"] = pd.to_numeric(temp_df["同比指数"], errors="coerce")
return temp_df
def index_volume_cflp(symbol: str = "月指数") -> pd.DataFrame:
"""
中国公路物流运量指数
http://index.0256.cn/expx.htm
:param symbol: choice of {"月指数", "季度指数", "年度指数"}
:type symbol: str
:return: 中国公路物流运量指数
:rtype: pandas.DataFrame
"""
symbol_map = {
"月指数": "3",
"季度指数": "4",
"年度指数": "5",
}
url = "http://index.0256.cn/volume_query.action"
params = {
"type": "1",
"marketId": "1",
"expTypeId": symbol_map[symbol],
"startDate1": "",
"endDate1": "",
"city": "",
"startDate3": "",
"endDate3": "",
}
headers = {
"Origin": "http://index.0256.cn",
"Referer": "http://index.0256.cn/expx.htm",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/90.0.4430.212 Safari/537.36",
}
r = requests.post(url, data=params, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(
[
data_json["chart1"]["xLebal"],
data_json["chart1"]["yLebal"],
data_json["chart2"]["yLebal"],
data_json["chart3"]["yLebal"],
]
).T
temp_df.columns = ["日期", "定基指数", "环比指数", "同比指数"]
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["定基指数"] = pd.to_numeric(temp_df["定基指数"], errors="coerce")
temp_df["环比指数"] = pd.to_numeric(temp_df["环比指数"], errors="coerce")
temp_df["同比指数"] = pd.to_numeric(temp_df["同比指数"], errors="coerce")
return temp_df
if __name__ == "__main__":
index_price_cflp_df = index_price_cflp(symbol="周指数")
print(index_price_cflp_df)
index_price_cflp_df = index_price_cflp(symbol="月指数")
print(index_price_cflp_df)
index_price_cflp_df = index_price_cflp(symbol="季度指数")
print(index_price_cflp_df)
index_price_cflp_df = index_price_cflp(symbol="年度指数")
print(index_price_cflp_df)
index_volume_cflp_df = index_volume_cflp(symbol="月指数")
print(index_volume_cflp_df)
index_volume_cflp_df = index_volume_cflp(symbol="季度指数")
print(index_volume_cflp_df)
index_volume_cflp_df = index_volume_cflp(symbol="年度指数")
print(index_volume_cflp_df)
@@ -0,0 +1,244 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2026/1/12 15:00
Desc: 国证指数
https://www.cnindex.com.cn/index.html
"""
import zipfile
from io import BytesIO
import pandas as pd
import requests
def index_all_cni() -> pd.DataFrame:
"""
国证指数-最近交易日的所有指数
https://www.cnindex.com.cn/zh_indices/sese/index.html?act_menu=1&index_type=-1
:return: 国证指数-所有指数
:rtype: pandas.DataFrame
"""
url = "https://www.cnindex.com.cn/index/indexList"
params = {
"channelCode": "-1",
"rows": "2000",
"pageNum": "1",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["rows"])
temp_df.columns = [
"_",
"_",
"指数代码",
"_",
"_",
"_",
"_",
"_",
"指数简称",
"_",
"_",
"_",
"样本数",
"收盘点位",
"涨跌幅",
"_",
"PE滚动",
"_",
"成交量",
"成交额",
"总市值",
"自由流通市值",
"_",
"_",
"_",
]
temp_df = temp_df[
[
"指数代码",
"指数简称",
"样本数",
"收盘点位",
"涨跌幅",
"PE滚动",
"成交量",
"成交额",
"总市值",
"自由流通市值",
]
]
temp_df["成交量"] = temp_df["成交量"] / 100000
temp_df["成交额"] = temp_df["成交额"] / 100000000
temp_df["总市值"] = temp_df["总市值"] / 100000000
temp_df["自由流通市值"] = temp_df["自由流通市值"] / 100000000
return temp_df
def index_hist_cni(
symbol: str = "399001", start_date: str = "20230114", end_date: str = "20240114"
) -> pd.DataFrame:
"""
指数历史行情数据
http://www.cnindex.com.cn/module/index-detail.html?act_menu=1&indexCode=399001
:param symbol: 指数代码
:type symbol: str
:param start_date: 开始时间
:type start_date: str
:param end_date: 结束时间
:type end_date: str
:return: 指数历史行情数据
:rtype: pandas.DataFrame
"""
start_date = "-".join([start_date[:4], start_date[4:6], start_date[6:]])
end_date = "-".join([end_date[:4], end_date[4:6], end_date[6:]])
url = "http://hq.cnindex.com.cn/market/market/getIndexDailyDataWithDataFormat"
params = {
"indexCode": symbol,
"startDate": start_date,
"endDate": end_date,
"frequency": "day",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["data"])
temp_df.columns = [
"日期",
"_",
"最高价",
"开盘价",
"最低价",
"收盘价",
"_",
"涨跌幅",
"成交额",
"成交量",
"_",
]
temp_df = temp_df[
[
"日期",
"开盘价",
"最高价",
"最低价",
"收盘价",
"涨跌幅",
"成交量",
"成交额",
]
]
temp_df["涨跌幅"] = temp_df["涨跌幅"].str.replace("%", "")
temp_df["涨跌幅"] = temp_df["涨跌幅"].astype("float")
temp_df["涨跌幅"] = temp_df["涨跌幅"] / 100
temp_df.sort_values(["日期"], inplace=True, ignore_index=True)
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["开盘价"] = pd.to_numeric(temp_df["开盘价"], errors="coerce")
temp_df["最高价"] = pd.to_numeric(temp_df["最高价"], errors="coerce")
temp_df["最低价"] = pd.to_numeric(temp_df["最低价"], errors="coerce")
temp_df["收盘价"] = pd.to_numeric(temp_df["收盘价"], errors="coerce")
temp_df["涨跌幅"] = pd.to_numeric(temp_df["涨跌幅"], errors="coerce")
temp_df["成交量"] = pd.to_numeric(temp_df["成交量"], errors="coerce")
temp_df["成交额"] = pd.to_numeric(temp_df["成交额"], errors="coerce")
return temp_df
def index_detail_cni(symbol: str = "399001") -> pd.DataFrame:
"""
国证指数-样本详情-指定日期的样本成份
https://www.cnindex.com.cn/module/index-detail.html?act_menu=1&indexCode=399001
:param symbol: 指数代码
:type symbol: str
:return: 指定日期的样本成份
:rtype: pandas.DataFrame
"""
import warnings
warnings.simplefilter(action="ignore", category=UserWarning)
url = "https://www.cnindex.com.cn/sample-detail/download-history"
params = {"indexcode": symbol}
r = requests.get(url, params=params)
temp_df = pd.read_excel(BytesIO(r.content))
temp_df["样本代码"] = temp_df["样本代码"].astype(str).str.zfill(6)
temp_df.columns = [
"日期",
"样本代码",
"样本简称",
"所属行业",
"总市值",
"权重",
]
temp_df["总市值"] = pd.to_numeric(temp_df["总市值"], errors="coerce")
temp_df["权重"] = pd.to_numeric(temp_df["权重"], errors="coerce")
return temp_df
def index_detail_hist_cni(symbol: str = "399001") -> pd.DataFrame:
"""
国证指数-样本详情-历史样本
https://www.cnindex.com.cn/module/index-detail.html?act_menu=1&indexCode=399001
:param symbol: 指数代码; "399001"
:type symbol: str
:return: 历史样本
:rtype: pandas.DataFrame
"""
url = "https://www.cnindex.com.cn/sample-detail/download-history"
params = {"indexcode": symbol}
r = requests.get(url, params=params)
temp_df = pd.read_excel(BytesIO(r.content))
temp_df["样本代码"] = temp_df["样本代码"].astype(str).str.zfill(6)
temp_df.columns = [
"日期",
"样本代码",
"样本简称",
"所属行业",
"总市值",
"权重",
]
temp_df["总市值"] = pd.to_numeric(temp_df["总市值"])
temp_df["权重"] = pd.to_numeric(temp_df["权重"])
return temp_df
def index_detail_hist_adjust_cni(symbol: str = "399005") -> pd.DataFrame:
"""
国证指数-样本详情-历史调样
http://www.cnindex.com.cn/module/index-detail.html?act_menu=1&indexCode=399005
:param symbol: 指数代码
:type symbol: str
:return: 历史调样
:rtype: pandas.DataFrame
"""
url = "http://www.cnindex.com.cn/sample-detail/download-adjustment"
params = {"indexcode": symbol}
r = requests.get(url, params=params)
try:
import warnings
with warnings.catch_warnings():
warnings.simplefilter(action="ignore", category=UserWarning)
temp_df = pd.read_excel(BytesIO(r.content), engine="openpyxl")
except zipfile.BadZipFile:
return pd.DataFrame()
temp_df["样本代码"] = temp_df["样本代码"].astype(str).str.zfill(6)
return temp_df
if __name__ == "__main__":
index_all_cni_df = index_all_cni()
print(index_all_cni_df)
index_hist_cni_df = index_hist_cni(
symbol="399005", start_date="20230114", end_date="20260328"
)
print(index_hist_cni_df)
index_detail_cni_df = index_detail_cni(symbol="399001")
print(index_detail_cni_df)
index_detail_hist_cni_df = index_detail_hist_cni(symbol="399101", date="202404")
print(index_detail_hist_cni_df)
index_detail_hist_adjust_cni_df = index_detail_hist_adjust_cni(symbol="399005")
print(index_detail_hist_adjust_cni_df)
@@ -0,0 +1,223 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/6/17 14:00
Desc: 股票指数成份股数据, 新浪有两个接口, 这里使用老接口:
新接口:https://vip.stock.finance.sina.com.cn/mkt/#zhishu_000001
老接口:https://vip.stock.finance.sina.com.cn/corp/view/vII_NewestComponent.php?page=1&indexid=399639
"""
import math
from io import BytesIO, StringIO
import pandas as pd
import requests
from bs4 import BeautifulSoup
from akshare.utils import demjson
def index_stock_cons_sina(symbol: str = "000300") -> pd.DataFrame:
"""
新浪新版股票指数成份页面, 目前该接口可获取指数数量较少
https://vip.stock.finance.sina.com.cn/mkt/#zhishu_000040
:param symbol: 指数代码
:type symbol: str
:return: 指数的成份股
:rtype: pandas.DataFrame
"""
if symbol == "000300":
symbol = "hs300"
url = (
"https://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php"
"/Market_Center.getHQNodeStockCountSimple"
)
params = {"node": f"{symbol}"}
r = requests.get(url, params=params)
page_num = math.ceil(int(r.json()) / 80) + 1
temp_df = pd.DataFrame()
for page in range(1, page_num):
url = "https://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData"
params = {
"page": str(page),
"num": "80",
"sort": "symbol",
"asc": "1",
"node": "hs300",
"symbol": "",
"_s_r_a": "init",
}
r = requests.get(url, params=params)
temp_df = pd.concat(
objs=[temp_df, pd.DataFrame(demjson.decode(r.text))], ignore_index=True
)
return temp_df
url = "https://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeDataSimple"
params = {
"page": 1,
"num": "3000",
"sort": "symbol",
"asc": "1",
"node": f"zhishu_{symbol}",
"_s_r_a": "setlen",
}
r = requests.get(url, params=params)
temp = pd.DataFrame(demjson.decode(r.text))
return temp
def index_stock_info() -> pd.DataFrame:
"""
聚宽-指数数据-指数列表
https://www.joinquant.com/data/dict/indexData
:return: 指数信息的数据框
:rtype: pandas.DataFrame
"""
url = "https://www.joinquant.com/data/dict/indexData"
r = requests.get(url)
r.encoding = "utf-8"
index_df = pd.read_html(StringIO(r.text))[0]
index_df["指数代码"] = index_df["指数代码"].str.split(".", expand=True)[0]
index_df.columns = ["index_code", "display_name", "publish_date", "-", "-"]
temp_df = index_df[["index_code", "display_name", "publish_date"]].copy()
return temp_df
def index_stock_cons(symbol: str = "399639") -> pd.DataFrame:
"""
最新股票指数的成份股目录
https://vip.stock.finance.sina.com.cn/corp/view/vII_NewestComponent.php?page=1&indexid=399639
:param symbol: 指数代码, 可以通过 ak.index_stock_info() 函数获取
:type symbol: str
:return: 最新股票指数的成份股目录
:rtype: pandas.DataFrame
"""
url = f"https://vip.stock.finance.sina.com.cn/corp/go.php/vII_NewestComponent/indexid/{symbol}.phtml"
r = requests.get(url)
r.encoding = "gb2312"
soup = BeautifulSoup(r.text, "lxml")
page_num = (
soup.find(attrs={"class": "table2"})
.find("td")
.find_all("a")[-1]["href"]
.split("page=")[-1]
.split("&")[0]
)
if page_num == "#":
temp_df = pd.read_html(StringIO(r.text), header=0, skiprows=1)[3].iloc[:, :3]
temp_df["品种代码"] = temp_df["品种代码"].astype(str).str.zfill(6)
return temp_df
temp_df = pd.DataFrame()
for page in range(1, int(page_num) + 1):
url = f"https://vip.stock.finance.sina.com.cn/corp/view/vII_NewestComponent.php?page={page}&indexid={symbol}"
r = requests.get(url)
r.encoding = "gb2312"
temp_df = pd.concat(
objs=[temp_df, pd.read_html(StringIO(r.text), header=1)[3]],
ignore_index=True,
)
temp_df = temp_df.iloc[:, :3]
temp_df["品种代码"] = temp_df["品种代码"].astype(str).str.zfill(6)
return temp_df
def index_stock_cons_csindex(symbol: str = "000300") -> pd.DataFrame:
"""
中证指数网站-成份股目录
https://www.csindex.com.cn/zh-CN/indices/index-detail/000300
:param symbol: 指数代码, 可以通过 ak.index_stock_info() 函数获取
:type symbol: str
:return: 最新指数的成份股
:rtype: pandas.DataFrame
"""
url = (
f"https://oss-ch.csindex.com.cn/static/"
f"html/csindex/public/uploads/file/autofile/cons/{symbol}cons.xls"
)
r = requests.get(url)
temp_df = pd.read_excel(BytesIO(r.content))
temp_df.columns = [
"日期",
"指数代码",
"指数名称",
"指数英文名称",
"成分券代码",
"成分券名称",
"成分券英文名称",
"交易所",
"交易所英文名称",
]
temp_df["日期"] = pd.to_datetime(
temp_df["日期"], format="%Y%m%d", errors="coerce"
).dt.date
temp_df["指数代码"] = temp_df["指数代码"].astype(str).str.zfill(6)
temp_df["成分券代码"] = temp_df["成分券代码"].astype(str).str.zfill(6)
return temp_df
def index_stock_cons_weight_csindex(symbol: str = "000300") -> pd.DataFrame:
"""
中证指数网站-样本权重
https://www.csindex.com.cn/zh-CN/indices/index-detail/000300
:param symbol: 指数代码, 可以通过 ak.index_stock_info() 接口获取
:type symbol: str
:return: 最新指数的成份股权重
:rtype: pandas.DataFrame
"""
url = (
f"https://oss-ch.csindex.com.cn/static/html/csindex/"
f"public/uploads/file/autofile/closeweight/{symbol}closeweight.xls"
)
r = requests.get(url)
temp_df = pd.read_excel(BytesIO(r.content))
temp_df.columns = [
"日期",
"指数代码",
"指数名称",
"指数英文名称",
"成分券代码",
"成分券名称",
"成分券英文名称",
"交易所",
"交易所英文名称",
"权重",
]
temp_df["日期"] = pd.to_datetime(
temp_df["日期"], format="%Y%m%d", errors="coerce"
).dt.date
temp_df["指数代码"] = temp_df["指数代码"].astype(str).str.zfill(6)
temp_df["成分券代码"] = temp_df["成分券代码"].astype(str).str.zfill(6)
temp_df["权重"] = pd.to_numeric(temp_df["权重"], errors="coerce")
return temp_df
def stock_a_code_to_symbol(symbol: str = "000300") -> str:
"""
输入股票代码判断股票市场
:param symbol: 股票代码
:type symbol: str
:return: 股票市场
:rtype: str
"""
if symbol.startswith("6") or symbol.startswith("900"):
return f"sh{symbol}"
else:
return f"sz{symbol}"
if __name__ == "__main__":
index_stock_cons_csindex_df = index_stock_cons_csindex(symbol="000300")
print(index_stock_cons_csindex_df)
index_stock_cons_weight_csindex_df = index_stock_cons_weight_csindex(
symbol="000300"
)
print(index_stock_cons_weight_csindex_df)
index_stock_cons_sina_df = index_stock_cons_sina(symbol="000300")
print(index_stock_cons_sina_df)
index_stock_cons_df = index_stock_cons(symbol="000300")
print(index_stock_cons_df)
@@ -0,0 +1,63 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2025/8/4 14:00
Desc: 中证指数网站-指数列表
网站:https://www.csindex.com.cn/#/indices/family/list?index_series=1
"""
import warnings
from io import BytesIO
import pandas as pd
import requests
def index_csindex_all() -> pd.DataFrame:
"""
中证指数网站-指数列表
https://www.csindex.com.cn/#/indices/family/list?index_series=1
Note: 但是不知道数据更新时间
:return: 最新指数的列表,
:rtype: pandas.DataFrame
"""
warnings.filterwarnings(
"ignore", category=UserWarning, message="Workbook contains no default style"
)
url = "https://www.csindex.com.cn/csindex-home/exportExcel/indexAll/CH"
headers = {
"Content-Type": "application/json;charset=UTF-8",
}
playloads = {
"sorter": {"sortField": "null", "sortOrder": None},
"pager": {"pageNum": 1, "pageSize": 10},
"indexFilter": {
"ifCustomized": None,
"ifTracked": None,
"ifWeightCapped": None,
"indexCompliance": None,
"hotSpot": None,
"indexClassify": None,
"currency": None,
"region": None,
"indexSeries": ["1"],
"undefined": None,
},
}
r = requests.post(url, json=playloads, headers=headers)
temp_df = pd.read_excel(BytesIO(r.content))
temp_df["基日"] = pd.to_datetime(
temp_df["基日"], format="%Y-%m-%d", errors="coerce"
).dt.date
temp_df["发布时间"] = pd.to_datetime(
temp_df["发布时间"], format="%Y-%m-%d", errors="coerce"
).dt.date
temp_df["指数代码"] = temp_df["指数代码"].astype(str).str.zfill(6)
return temp_df
if __name__ == "__main__":
index_csindex_all_df = index_csindex_all()
print(index_csindex_all_df)
@@ -0,0 +1,621 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
"""
Date: 2025/8/7 18:30
Desc: 财新数据-指数报告-数字经济指数
https://yun.ccxe.com.cn/indices/dei
"""
import pandas as pd
import requests
def index_pmi_com_cx() -> pd.DataFrame:
"""
财新数据-指数报告-财新中国 PMI-综合 PMI
https://yun.ccxe.com.cn/indices/pmi
:return: 财新中国 PMI-综合 PMI
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "com"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "综合PMI", "日期"]
temp_df = temp_df[
[
"日期",
"综合PMI",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_pmi_man_cx() -> pd.DataFrame:
"""
财新数据-指数报告-财新中国 PMI-制造业 PMI
https://yun.ccxe.com.cn/indices/pmi
:return: 财新中国 PMI-制造业 PMI
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "man"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "制造业PMI", "日期"]
temp_df = temp_df[
[
"日期",
"制造业PMI",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_pmi_ser_cx() -> pd.DataFrame:
"""
财新数据-指数报告-财新中国 PMI-服务业 PMI
https://yun.ccxe.com.cn/indices/pmi
:return: 财新中国 PMI-服务业 PMI
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "ser"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "服务业PMI", "日期"]
temp_df = temp_df[
[
"日期",
"服务业PMI",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_dei_cx() -> pd.DataFrame:
"""
财新数据-指数报告-数字经济指数
https://yun.ccxe.com.cn/indices/dei
:return: 数字经济指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "dei"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "数字经济指数", "日期"]
temp_df = temp_df[
[
"日期",
"数字经济指数",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_ii_cx() -> pd.DataFrame:
"""
财新数据-指数报告-产业指数
https://yun.ccxe.com.cn/indices/dei
:return: 产业指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "ii"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "产业指数", "日期"]
temp_df = temp_df[
[
"日期",
"产业指数",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_si_cx() -> pd.DataFrame:
"""
财新数据-指数报告-溢出指数
https://yun.ccxe.com.cn/indices/dei
:return: 溢出指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "si"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "溢出指数", "日期"]
temp_df = temp_df[
[
"日期",
"溢出指数",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_fi_cx() -> pd.DataFrame:
"""
财新数据-指数报告-融合指数
https://yun.ccxe.com.cn/indices/dei
:return: 融合指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "fi"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "融合指数", "日期"]
temp_df = temp_df[
[
"日期",
"融合指数",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_bi_cx() -> pd.DataFrame:
"""
财新数据-指数报告-基础指数
https://yun.ccxe.com.cn/indices/dei
:return: 基础指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "bi"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "基础指数", "日期"]
temp_df = temp_df[
[
"日期",
"基础指数",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_nei_cx() -> pd.DataFrame:
"""
财新数据-指数报告-中国新经济指数
https://yun.ccxe.com.cn/indices/nei
:return: 中国新经济指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "nei"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "中国新经济指数", "日期"]
temp_df = temp_df[
[
"日期",
"中国新经济指数",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_li_cx() -> pd.DataFrame:
"""
财新数据-指数报告-劳动力投入指数
https://yun.ccxe.com.cn/indices/nei
:return: 劳动力投入指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "li"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "劳动力投入指数", "日期"]
temp_df = temp_df[
[
"日期",
"劳动力投入指数",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_ci_cx() -> pd.DataFrame:
"""
财新数据-指数报告-资本投入指数
https://yun.ccxe.com.cn/indices/nei
:return: 资本投入指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "ci"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "资本投入指数", "日期"]
temp_df = temp_df[
[
"日期",
"资本投入指数",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_ti_cx() -> pd.DataFrame:
"""
财新数据-指数报告-科技投入指数
https://yun.ccxe.com.cn/indices/nei
:return: 科技投入指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "ti"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "科技投入指数", "日期"]
temp_df = temp_df[
[
"日期",
"科技投入指数",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_neaw_cx() -> pd.DataFrame:
"""
财新数据-指数报告-新经济行业入职平均工资水平
https://yun.ccxe.com.cn/indices/nei
:return: 新经济行业入职平均工资水平
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "neaw"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "新经济行业入职平均工资水平", "日期"]
temp_df = temp_df[
[
"日期",
"新经济行业入职平均工资水平",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_awpr_cx() -> pd.DataFrame:
"""
财新数据-指数报告-新经济入职工资溢价水平
https://yun.ccxe.com.cn/indices/nei
:return: 新经济入职工资溢价水平
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {"type": "awpr"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "新经济入职工资溢价水平", "日期"]
temp_df = temp_df[
[
"日期",
"新经济入职工资溢价水平",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_cci_cx() -> pd.DataFrame:
"""
财新数据-指数报告-大宗商品指数
https://yun.ccxe.com.cn/indices/nei
:return: 大宗商品指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {
"type": "cci",
"code": "1000050",
"month": "-1",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化值", "大宗商品指数", "日期"]
temp_df = temp_df[
[
"日期",
"大宗商品指数",
"变化值",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_qli_cx() -> pd.DataFrame:
"""
财新数据-指数报告-高质量因子
https://yun.ccxe.com.cn/indices/qli
:return: 高质量因子
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {
"type": "qli",
"code": "1000050",
"month": "-1",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化幅度", "高质量因子指数", "日期"]
temp_df = temp_df[
[
"日期",
"高质量因子指数",
"变化幅度",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_ai_cx() -> pd.DataFrame:
"""
财新数据-指数报告-AI策略指数
https://yun.ccxe.com.cn/indices/ai
:return: AI策略指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {
"type": "ai",
"code": "1000050",
"month": "-1",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化幅度", "AI策略指数", "日期"]
temp_df = temp_df[
[
"日期",
"AI策略指数",
"变化幅度",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_bei_cx() -> pd.DataFrame:
"""
财新数据-指数报告-基石经济指数
https://yun.ccxe.com.cn/indices/bei
:return: 基石经济指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {
"type": "ind",
"code": "930927",
"month": "-1",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化幅度", "基石经济指数", "日期"]
temp_df = temp_df[
[
"日期",
"基石经济指数",
"变化幅度",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
def index_neei_cx() -> pd.DataFrame:
"""
财新数据-指数报告-新动能指数
https://yun.ccxe.com.cn/indices/neei
:return: 新动能指数
:rtype: pandas.DataFrame
"""
url = "https://yun.ccxe.com.cn/api/index/pro/cxIndexTrendInfo"
params = {
"type": "ind",
"code": "930928",
"month": "1",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = ["变化幅度", "新动能指数", "日期"]
temp_df = temp_df[
[
"日期",
"新动能指数",
"变化幅度",
]
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms", utc=True)
.dt.tz_convert("Asia/Shanghai")
.dt.date
)
return temp_df
if __name__ == "__main__":
index_pmi_com_cx_df = index_pmi_com_cx()
print(index_pmi_com_cx_df)
index_pmi_man_cx_df = index_pmi_man_cx()
print(index_pmi_man_cx_df)
index_pmi_ser_cx_df = index_pmi_ser_cx()
print(index_pmi_ser_cx_df)
index_dei_cx_df = index_dei_cx()
print(index_dei_cx_df)
index_ii_cx_df = index_ii_cx()
print(index_ii_cx_df)
index_si_cx_df = index_si_cx()
print(index_si_cx_df)
index_fi_cx_df = index_fi_cx()
print(index_fi_cx_df)
index_bi_cx_df = index_bi_cx()
print(index_bi_cx_df)
index_nei_cx_df = index_nei_cx()
print(index_nei_cx_df)
index_li_cx_df = index_li_cx()
print(index_li_cx_df)
index_ci_cx_df = index_ci_cx()
print(index_ci_cx_df)
index_ti_cx_df = index_ti_cx()
print(index_ti_cx_df)
index_neaw_cx_df = index_neaw_cx()
print(index_neaw_cx_df)
index_awpr_cx_df = index_awpr_cx()
print(index_awpr_cx_df)
index_cci_cx_df = index_cci_cx()
print(index_cci_cx_df)
index_qli_cx_df = index_qli_cx()
print(index_qli_cx_df)
index_ai_cx_df = index_ai_cx()
print(index_ai_cx_df)
index_bei_cx_df = index_bei_cx()
print(index_bei_cx_df)
index_neei_cx_df = index_neei_cx()
print(index_neei_cx_df)
@@ -0,0 +1,82 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/9/26 18:00
Desc: Drewry 集装箱指数
https://www.drewry.co.uk/supply-chain-advisors/supply-chain-expertise/world-container-index-assessed-by-drewry
https://infogram.com/world-container-index-1h17493095xl4zj
"""
import pandas as pd
import requests
from bs4 import BeautifulSoup
from akshare.utils import demjson
def drewry_wci_index(symbol: str = "composite") -> pd.DataFrame:
"""
Drewry 集装箱指数
https://infogram.com/world-container-index-1h17493095xl4zj
:param symbol: choice of {"composite", "shanghai-rotterdam", "rotterdam-shanghai", "shanghai-los angeles",
"los angeles-shanghai", "shanghai-genoa", "new york-rotterdam", "rotterdam-new york"}
:type symbol: str
:return: Drewry 集装箱指数
:rtype: pandas.DataFrame
"""
symbol_map = {
"composite": 0,
"shanghai-rotterdam": 1,
"rotterdam-shanghai": 2,
"shanghai-los angeles": 3,
"los angeles-shanghai": 4,
"shanghai-genoa": 5,
"new york-rotterdam": 6,
"rotterdam-new york": 7,
}
url = "https://infogram.com/world-container-index-1h17493095xl4zj"
r = requests.get(url)
soup = BeautifulSoup(r.text, features="lxml")
data_text = soup.find_all("script")[-4].string.strip("window.infographicData=")[:-1]
data_json = demjson.decode(data_text)
data_json_need = data_json["elements"]["content"]["content"]["entities"][
"7a55585f-3fb3-44e6-9b54-beea1cd20b4d"
]["data"][symbol_map[symbol]]
date_list = [item[0]["value"] for item in data_json_need[1:]]
try:
value_list = [item[1]["value"] for item in data_json_need[1:]]
except TypeError:
value_list = [item[1]["value"] for item in data_json_need[1:-1]]
temp_df = pd.DataFrame([date_list, value_list]).T
temp_df.columns = ["date", "wci"]
temp_df["date"] = pd.to_datetime(
temp_df["date"], format="%d-%b-%y", errors="coerce"
).dt.date
temp_df["wci"] = pd.to_numeric(temp_df["wci"], errors="coerce")
return temp_df
if __name__ == "__main__":
drewry_wci_index_df = drewry_wci_index(symbol="composite")
print(drewry_wci_index_df)
drewry_wci_index_df = drewry_wci_index(symbol="shanghai-rotterdam")
print(drewry_wci_index_df)
drewry_wci_index_df = drewry_wci_index(symbol="rotterdam-shanghai")
print(drewry_wci_index_df)
drewry_wci_index_df = drewry_wci_index(symbol="shanghai-los angeles")
print(drewry_wci_index_df)
drewry_wci_index_df = drewry_wci_index(symbol="los angeles-shanghai")
print(drewry_wci_index_df)
drewry_wci_index_df = drewry_wci_index(symbol="shanghai-genoa")
print(drewry_wci_index_df)
drewry_wci_index_df = drewry_wci_index(symbol="new york-rotterdam")
print(drewry_wci_index_df)
drewry_wci_index_df = drewry_wci_index(symbol="rotterdam-new york")
print(drewry_wci_index_df)
@@ -0,0 +1,62 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2023/6/19 18:16
Desc: 浙江省排污权交易指数
https://zs.zjpwq.net/
"""
import pandas as pd
import requests
def index_eri(symbol: str = "月度") -> pd.DataFrame:
"""
浙江省排污权交易指数
https://zs.zjpwq.net
:param symbol: choice of {"月度", "季度"}
:type symbol: str
:return: 浙江省排污权交易指数
:rtype: pandas.DataFrame
"""
symbol_map = {
"月度": "MONTH",
"季度": "QUARTER",
}
url = "https://zs.zjpwq.net/pwq-index-webapi/indexData"
params = {
"cycle": symbol_map[symbol],
"regionId": "1",
"structId": "1",
"pageSize": "5000",
"indexId": "1",
"orderBy": "stage.publishTime",
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
}
r = requests.get(url, params=params, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
index_value = temp_df["indexValue"].tolist()
index_time = [item["stage"]["publishTime"] for item in data_json["data"]]
big_df = pd.DataFrame([index_time, index_value], index=["日期", "交易指数"]).T
url = "https://zs.zjpwq.net/pwq-index-webapi/dataStatistics"
r = requests.get(url, params=params, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
big_df["成交量"] = temp_df["totalQuantity"].tolist()
big_df["成交额"] = temp_df["totalCost"].tolist()
big_df["日期"] = pd.to_datetime(big_df["日期"], errors="coerce").dt.date
big_df["交易指数"] = pd.to_numeric(big_df["交易指数"], errors="coerce")
big_df["成交量"] = pd.to_numeric(big_df["成交量"], errors="coerce")
big_df["成交额"] = pd.to_numeric(big_df["成交额"], errors="coerce")
return big_df
if __name__ == "__main__":
index_eri_df = index_eri(symbol="月度")
print(index_eri_df)
index_eri_df = index_eri(symbol="季度")
print(index_eri_df)
@@ -0,0 +1,167 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2025/3/7 17:00
Desc: 东方财富网-行情中心-全球指数
https://quote.eastmoney.com/center/gridlist.html#global_qtzs
"""
import pandas as pd
import requests
from akshare.index.cons import index_global_em_symbol_map
def index_global_spot_em() -> pd.DataFrame:
"""
东方财富网-行情中心-全球指数-实时行情数据
https://quote.eastmoney.com/center/gridlist.html#global_qtzs
:return: 实时行情数据
:rtype: pandas.DataFrame
"""
url = "https://push2.eastmoney.com/api/qt/clist/get"
params = {
"np": "2",
"fltt": "1",
"invt": "2",
"fs": "i:1.000001,i:0.399001,i:0.399005,i:0.399006,i:1.000300,i:100.HSI,i:100.HSCEI,i:124.HSCCI,"
"i:100.TWII,i:100.N225,i:100.KOSPI200,i:100.KS11,i:100.STI,i:100.SENSEX,i:100.KLSE,i:100.SET,"
"i:100.PSI,i:100.KSE100,i:100.VNINDEX,i:100.JKSE,i:100.CSEALL,i:100.SX5E,i:100.FTSE,i:100.MCX,"
"i:100.AXX,i:100.FCHI,i:100.GDAXI,i:100.RTS,i:100.IBEX,i:100.PSI20,i:100.OMXC20,i:100.BFX,"
"i:100.AEX,i:100.WIG,i:100.OMXSPI,i:100.SSMI,i:100.HEX,i:100.OSEBX,i:100.ATX,i:100.MIB,"
"i:100.ASE,i:100.ICEXI,i:100.PX,i:100.ISEQ,i:100.DJIA,i:100.SPX,i:100.NDX,i:100.TSX,"
"i:100.BVSP,i:100.MXX,i:100.AS51,i:100.AORD,i:100.NZ50,i:100.UDI,i:100.BDI,i:100.CRB",
"fields": "f12,f13,f14,f292,f1,f2,f4,f3,f152,f17,f18,f15,f16,f7,f124",
"fid": "f3",
"pn": "1",
"pz": "200",
"po": "1",
"dect": "1",
"wbp2u": "|0|0|0|web",
}
r = requests.get(url=url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["diff"]).T
temp_df.reset_index(inplace=True)
temp_df["index"] = temp_df["index"].astype(int) + 1
temp_df.rename(
columns={
"index": "序号",
"f12": "代码",
"f14": "名称",
"f17": "开盘价",
"f4": "涨跌额",
"f3": "涨跌幅",
"f2": "最新价",
"f15": "最高价",
"f16": "最低价",
"f18": "昨收价",
"f7": "振幅",
"f124": "最新行情时间",
},
inplace=True,
)
temp_df = temp_df[
[
"序号",
"代码",
"名称",
"最新价",
"涨跌额",
"涨跌幅",
"开盘价",
"最高价",
"最低价",
"昨收价",
"振幅",
"最新行情时间",
]
]
temp_df["最新行情时间"] = pd.to_datetime(
temp_df["最新行情时间"], unit="s", utc=True, errors="coerce"
).dt.tz_convert("Asia/Shanghai")
temp_df["最新行情时间"] = temp_df["最新行情时间"].dt.strftime("%Y-%m-%d %H:%M:%S")
temp_df["最新价"] = pd.to_numeric(temp_df["最新价"], errors="coerce") / 100
temp_df["涨跌额"] = pd.to_numeric(temp_df["涨跌额"], errors="coerce") / 100
temp_df["涨跌幅"] = pd.to_numeric(temp_df["涨跌幅"], errors="coerce") / 100
temp_df["开盘价"] = pd.to_numeric(temp_df["开盘价"], errors="coerce") / 100
temp_df["最高价"] = pd.to_numeric(temp_df["最高价"], errors="coerce") / 100
temp_df["最低价"] = pd.to_numeric(temp_df["最低价"], errors="coerce") / 100
temp_df["昨收价"] = pd.to_numeric(temp_df["昨收价"], errors="coerce") / 100
temp_df["振幅"] = pd.to_numeric(temp_df["振幅"], errors="coerce") / 100
return temp_df
def index_global_hist_em(symbol: str = "美元指数") -> pd.DataFrame:
"""
东方财富网-行情中心-全球指数-历史行情数据
https://quote.eastmoney.com/gb/zsUDI.html
:param symbol: 指数名称;可以通过 ak.index_global_spot_em() 获取
:type symbol: str
:return: 历史行情数据
:rtype: pandas.DataFrame
"""
url = "https://push2his.eastmoney.com/api/qt/stock/kline/get"
params = {
"secid": f"{index_global_em_symbol_map[symbol]['market']}.{index_global_em_symbol_map[symbol]['code']}",
"klt": "101",
"fqt": "1",
"lmt": "50000",
"end": "20500000",
"iscca": "1",
"fields1": "f1,f2,f3,f4,f5,f6,f7,f8",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61,f62,f63,f64",
"ut": "f057cbcbce2a86e2866ab8877db1d059",
"forcect": "1",
}
r = requests.get(url=url, params=params)
data_json = r.json()
temp_df = pd.DataFrame([item.split(",") for item in data_json["data"]["klines"]])
temp_df["code"] = data_json["data"]["code"]
temp_df["name"] = data_json["data"]["name"]
temp_df.columns = [
"日期",
"今开",
"最新价",
"最高",
"最低",
"-",
"-",
"振幅",
"-",
"-",
"-",
"-",
"-",
"-",
"代码",
"名称",
]
temp_df = temp_df[
[
"日期",
"代码",
"名称",
"今开",
"最新价",
"最高",
"最低",
"振幅",
]
]
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["今开"] = pd.to_numeric(temp_df["今开"], errors="coerce")
temp_df["最新价"] = pd.to_numeric(temp_df["最新价"], errors="coerce")
temp_df["最高"] = pd.to_numeric(temp_df["最高"], errors="coerce")
temp_df["最低"] = pd.to_numeric(temp_df["最低"], errors="coerce")
temp_df["振幅"] = pd.to_numeric(temp_df["振幅"], errors="coerce")
return temp_df
if __name__ == "__main__":
index_global_spot_em_df = index_global_spot_em()
print(index_global_spot_em_df)
index_global_hist_em_df = index_global_hist_em(symbol="美元指数")
print(index_global_hist_em_df)
@@ -0,0 +1,82 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2025/3/7 17:00
Desc: 新浪财经-行情中心-环球市场
https://finance.sina.com.cn/stock/globalindex/quotes/UKX
"""
import pandas as pd
import requests
from akshare.index.cons import index_global_sina_symbol_map
def index_global_name_table() -> pd.DataFrame:
"""
新浪财经-行情中心-环球市场-名称代码映射表
https://finance.sina.com.cn/stock/globalindex/quotes/UKX
:return: 名称代码映射表
:rtype: pandas.DataFrame
"""
temp_df = pd.DataFrame.from_dict(
index_global_sina_symbol_map, orient="index", columns=["代码"]
)
temp_df.index.name = "指数名称"
temp_df.reset_index(inplace=True)
return temp_df
def index_global_hist_sina(symbol: str = "OMX") -> pd.DataFrame:
"""
新浪财经-行情中心-环球市场-历史行情
https://finance.sina.com.cn/stock/globalindex/quotes/UKX
:param symbol: 指数名称;可以通过 ak.index_global_name_table() 获取
:type symbol: str
:return: 环球市场历史行情
:rtype: pandas.DataFrame
"""
url = "https://gi.finance.sina.com.cn/hq/daily"
params = {
"symbol": index_global_sina_symbol_map[symbol],
"num": "10000",
}
r = requests.get(url=url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["result"]["data"])
temp_df.rename(
columns={
"d": "date",
"o": "open",
"h": "high",
"l": "low",
"c": "close",
"v": "volume",
},
inplace=True,
)
temp_df = temp_df[
[
"date",
"open",
"high",
"low",
"close",
"volume",
]
]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
temp_df["volume"] = pd.to_numeric(temp_df["volume"], errors="coerce")
return temp_df
if __name__ == "__main__":
index_global_name_table_df = index_global_name_table()
print(index_global_name_table_df)
index_global_hist_sina_df = index_global_hist_sina(symbol="瑞士股票指数")
print(index_global_hist_sina_df)
@@ -0,0 +1,50 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/3/21 11:16
Desc: 行情宝
https://hqb.nxin.com/pigindex/index.shtml
"""
import pandas as pd
import requests
def index_hog_spot_price() -> pd.DataFrame:
"""
行情宝-生猪市场价格指数
https://hqb.nxin.com/pigindex/index.shtml
:return: 生猪市场价格指数
:rtype: pandas.DataFrame
"""
url = "https://hqb.nxin.com/pigindex/getPigIndexChart.shtml"
params = {"regionId": "0"}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = [
"日期",
"指数",
"4个月均线",
"6个月均线",
"12个月均线",
"预售均价",
"成交均价",
"成交均重",
]
temp_df["日期"] = (
pd.to_datetime(temp_df["日期"], unit="ms") + pd.Timedelta(hours=8)
).dt.date
temp_df["指数"] = pd.to_numeric(temp_df["指数"], errors="coerce")
temp_df["4个月均线"] = pd.to_numeric(temp_df["4个月均线"], errors="coerce")
temp_df["6个月均线"] = pd.to_numeric(temp_df["6个月均线"], errors="coerce")
temp_df["12个月均线"] = pd.to_numeric(temp_df["12个月均线"], errors="coerce")
temp_df["预售均价"] = pd.to_numeric(temp_df["预售均价"], errors="coerce")
temp_df["成交均价"] = pd.to_numeric(temp_df["成交均价"], errors="coerce")
temp_df["成交均重"] = pd.to_numeric(temp_df["成交均重"], errors="coerce")
return temp_df
if __name__ == "__main__":
index_hog_spot_price_df = index_hog_spot_price()
print(index_hog_spot_price_df)
@@ -0,0 +1,101 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2023/5/18 17:10
Desc: 中国柯桥纺织指数
http://www.kqindex.cn/flzs/jiage
"""
import pandas as pd
import requests
from tqdm import tqdm
def index_kq_fz(symbol: str = "价格指数") -> pd.DataFrame:
"""
中国柯桥纺织指数
http://www.kqindex.cn/flzs/jiage
:param symbol: choice of {'价格指数', '景气指数', '外贸指数'}
:type symbol: str
:return: 中国柯桥纺织指数
:rtype: pandas.DataFrame
"""
symbol_map = {
"价格指数": "1_1",
"景气指数": "1_2",
"外贸指数": "2",
}
url = "http://www.kqindex.cn/flzs/table_data"
params = {
"category": "0",
"start": "",
"end": "",
"indexType": f"{symbol_map[symbol]}",
"pageindex": "1",
}
r = requests.get(url, params=params)
data_json = r.json()
page_num = data_json["page"]
big_df = pd.DataFrame()
for page in tqdm(range(1, page_num + 1), leave=False):
params = {
"category": "0",
"start": "",
"end": "",
"indexType": f"{symbol_map[symbol]}",
"pageindex": page,
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["result"])
big_df = pd.concat([big_df, temp_df], ignore_index=True)
if symbol == "价格指数":
big_df.columns = [
"期次",
"指数",
"涨跌幅",
]
big_df["期次"] = pd.to_datetime(big_df["期次"])
big_df["指数"] = pd.to_numeric(big_df["指数"], errors="coerce")
big_df["涨跌幅"] = pd.to_numeric(big_df["涨跌幅"], errors="coerce")
elif symbol == "景气指数":
big_df.columns = [
"期次",
"总景气指数",
"涨跌幅",
"流通景气指数",
"生产景气指数",
]
big_df["总景气指数"] = pd.to_numeric(big_df["总景气指数"], errors="coerce")
big_df["涨跌幅"] = pd.to_numeric(big_df["涨跌幅"], errors="coerce")
big_df["流通景气指数"] = pd.to_numeric(big_df["流通景气指数"], errors="coerce")
big_df["生产景气指数"] = pd.to_numeric(big_df["生产景气指数"], errors="coerce")
elif symbol == "外贸指数":
big_df.columns = [
"期次",
"价格指数",
"价格指数-涨跌幅",
"景气指数",
"景气指数-涨跌幅",
]
big_df["价格指数"] = pd.to_numeric(big_df["价格指数"], errors="coerce")
big_df["价格指数-涨跌幅"] = pd.to_numeric(
big_df["价格指数-涨跌幅"], errors="coerce"
)
big_df["景气指数"] = pd.to_numeric(big_df["景气指数"], errors="coerce")
big_df["景气指数-涨跌幅"] = pd.to_numeric(
big_df["景气指数-涨跌幅"], errors="coerce"
)
big_df.sort_values(["期次"], inplace=True, ignore_index=True)
return big_df
if __name__ == "__main__":
index_kq_fz_df = index_kq_fz(symbol="价格指数")
print(index_kq_fz_df)
index_kq_fz_df = index_kq_fz(symbol="景气指数")
print(index_kq_fz_df)
index_kq_fz_df = index_kq_fz(symbol="外贸指数")
print(index_kq_fz_df)
@@ -0,0 +1,97 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2023/6/13 22:05
Desc: 柯桥时尚指数
http://ss.kqindex.cn:9559/rinder_web_kqsszs/index/index_page.do
"""
import pandas as pd
import requests
def index_kq_fashion(symbol: str = "时尚创意指数") -> pd.DataFrame:
"""
柯桥时尚指数
http://ss.kqindex.cn:9559/rinder_web_kqsszs/index/index_page.do
:param symbol: choice of {'柯桥时尚指数', '时尚创意指数', '时尚设计人才数', '新花型推出数', '创意产品成交数', '创意企业数量', '时尚活跃度指数', '电商运行数', '时尚平台拓展数', '新产品销售额占比', '企业合作占比', '品牌传播费用', '时尚推广度指数', '国际交流合作次数', '企业参展次数', '外商驻点数量变化', '时尚评价指数'}
:type symbol: str
:return: 柯桥时尚指数及其子项数据
:rtype: pandas.DataFrame
"""
url = "http://api.idx365.com/index/project/34/data"
symbol_map = {
"柯桥时尚指数": "root",
"时尚创意指数": "01",
"时尚设计人才数": "0101",
"新花型推出数": "0102",
"创意产品成交数": "0103",
"创意企业数量": "0104",
"时尚活跃度指数": "02",
"电商运行数": "0201",
"时尚平台拓展数": "0201",
"新产品销售额占比": "0201",
"企业合作占比": "0201",
"品牌传播费用": "0201",
"时尚推广度指数": "03",
"国际交流合作次数": "0301",
"企业参展次数": "0302",
"外商驻点数量变化": "0302",
"时尚评价指数": "04",
}
params = {"structCode": symbol_map[symbol]}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.rename(
columns={
"id": "_",
"indexValue": "指数",
"lastValue": "_",
"projId": "_",
"publishTime": "日期",
"sameValue": "_",
"stageId": "_",
"structCode": "_",
"structName": "_",
"version": "_",
},
inplace=True,
)
temp_df = temp_df[
[
"日期",
"指数",
]
]
temp_df["日期"] = pd.to_datetime(temp_df["日期"]).dt.date
temp_df.sort_values("日期", inplace=True)
temp_df["涨跌值"] = temp_df["指数"].diff()
temp_df["涨跌幅"] = temp_df["指数"].pct_change()
temp_df.sort_values("日期", ascending=True, inplace=True, ignore_index=True)
return temp_df
if __name__ == "__main__":
for item in [
"柯桥时尚指数",
"时尚创意指数",
"时尚设计人才数",
"新花型推出数",
"创意产品成交数",
"创意企业数量",
"时尚活跃度指数",
"电商运行数",
"时尚平台拓展数",
"新产品销售额占比",
"企业合作占比",
"品牌传播费用",
"时尚推广度指数",
"国际交流合作次数",
"企业参展次数",
"外商驻点数量变化",
"时尚评价指数",
]:
index_kq_fashion_df = index_kq_fashion(symbol=item)
print(item)
print(index_kq_fashion_df)
@@ -0,0 +1,441 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
"""
Date: 2026/3/22 21:00
Desc: 50 ETF 期权波动率指数 QVIX
300 ETF 期权波动率指数 QVIX
http://1.optbbs.com/s/vix.shtml?50ETF
http://1.optbbs.com/s/vix.shtml?300ETF
"""
import pandas as pd
from functools import lru_cache
@lru_cache
def __get_optbbs_daily() -> pd.DataFrame:
"""
读取原始数据
http://1.optbbs.com/d/csv/d/k.csv
:return: 原始数据
:rtype: pandas.DataFrame
"""
url = "http://1.optbbs.com/d/csv/d/k.csv"
temp_df = pd.read_csv(url, encoding="gbk")
return temp_df
def index_option_50etf_qvix() -> pd.DataFrame:
"""
50ETF 期权波动率指数 QVIX
http://1.optbbs.com/s/vix.shtml?50ETF
:return: 50ETF 期权波动率指数 QVIX
:rtype: pandas.DataFrame
"""
temp_df = __get_optbbs_daily().iloc[:, :5]
temp_df.columns = [
"date",
"open",
"high",
"low",
"close",
]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
return temp_df
def index_option_50etf_min_qvix() -> pd.DataFrame:
"""
50 ETF 期权波动率指数 QVIX
http://1.optbbs.com/s/vix.shtml?50ETF
:return: 50 ETF 期权波动率指数 QVIX
:rtype: pandas.DataFrame
"""
url = "http://1.optbbs.com/d/csv/d/vix50.csv"
temp_df = pd.read_csv(url).iloc[:, :2]
temp_df.columns = [
"time",
"qvix",
]
temp_df.loc[:, "qvix"] = pd.to_numeric(temp_df["qvix"], errors="coerce")
return temp_df
def index_option_300etf_qvix() -> pd.DataFrame:
"""
300 ETF 期权波动率指数 QVIX
http://1.optbbs.com/s/vix.shtml?300ETF
:return: 300 ETF 期权波动率指数 QVIX
:rtype: pandas.DataFrame
"""
temp_df = __get_optbbs_daily().iloc[:, [0, 9, 10, 11, 12]]
temp_df.columns = [
"date",
"open",
"high",
"low",
"close",
]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
return temp_df
def index_option_300etf_min_qvix() -> pd.DataFrame:
"""
300 ETF 期权波动率指数 QVIX-分时
http://1.optbbs.com/s/vix.shtml?300ETF
:return: 300 ETF 期权波动率指数 QVIX-分时
:rtype: pandas.DataFrame
"""
url = "http://1.optbbs.com/d/csv/d/vix300.csv"
temp_df = pd.read_csv(url).iloc[:, :2]
temp_df.columns = [
"time",
"qvix",
]
temp_df.loc[:, "qvix"] = pd.to_numeric(temp_df["qvix"], errors="coerce")
return temp_df
def index_option_500etf_qvix() -> pd.DataFrame:
"""
500 ETF 期权波动率指数 QVIX
http://1.optbbs.com/s/vix.shtml?500ETF
:return: 500 ETF 期权波动率指数 QVIX
:rtype: pandas.DataFrame
"""
temp_df = __get_optbbs_daily().iloc[:, [0, 67, 68, 69, 70]]
temp_df.columns = [
"date",
"open",
"high",
"low",
"close",
]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
return temp_df
def index_option_500etf_min_qvix() -> pd.DataFrame:
"""
500 ETF 期权波动率指数 QVIX-分时
http://1.optbbs.com/s/vix.shtml?500ETF
:return: 500 ETF 期权波动率指数 QVIX-分时
:rtype: pandas.DataFrame
"""
url = "http://1.optbbs.com/d/csv/d/vix500.csv"
temp_df = pd.read_csv(url).iloc[:, :2]
temp_df.columns = [
"time",
"qvix",
]
temp_df.loc[:, "qvix"] = pd.to_numeric(temp_df["qvix"], errors="coerce")
return temp_df
def index_option_cyb_qvix() -> pd.DataFrame:
"""
创业板 期权波动率指数 QVIX
http://1.optbbs.com/s/vix.shtml?CYB
:return: 创业板 期权波动率指数 QVIX
:rtype: pandas.DataFrame
"""
temp_df = __get_optbbs_daily().iloc[:, [0, 71, 72, 73, 74]]
temp_df.columns = [
"date",
"open",
"high",
"low",
"close",
]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
return temp_df
def index_option_cyb_min_qvix() -> pd.DataFrame:
"""
创业板 期权波动率指数 QVIX-分时
http://1.optbbs.com/s/vix.shtml?CYB
:return: 创业板 期权波动率指数 QVIX-分时
:rtype: pandas.DataFrame
"""
url = "http://1.optbbs.com/d/csv/d/vixcyb.csv"
temp_df = pd.read_csv(url).iloc[:, :2]
temp_df.columns = [
"time",
"qvix",
]
temp_df.loc[:, "qvix"] = pd.to_numeric(temp_df["qvix"], errors="coerce")
return temp_df
def index_option_kcb_qvix() -> pd.DataFrame:
"""
科创板 期权波动率指数 QVIX
http://1.optbbs.com/s/vix.shtml?KCB
:return: 科创板 期权波动率指数 QVIX
:rtype: pandas.DataFrame
"""
temp_df = __get_optbbs_daily().iloc[:, [0, 83, 84, 85, 86]]
temp_df.columns = [
"date",
"open",
"high",
"low",
"close",
]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
return temp_df
def index_option_kcb_min_qvix() -> pd.DataFrame:
"""
科创板 期权波动率指数 QVIX-分时
http://1.optbbs.com/s/vix.shtml?KCB
:return: 科创板 期权波动率指数 QVIX-分时
:rtype: pandas.DataFrame
"""
url = "http://1.optbbs.com/d/csv/d/vixkcb.csv"
temp_df = pd.read_csv(url).iloc[:, :2]
temp_df.columns = [
"time",
"qvix",
]
temp_df.loc[:, "qvix"] = pd.to_numeric(temp_df["qvix"], errors="coerce")
return temp_df
def index_option_100etf_qvix() -> pd.DataFrame:
"""
深证100ETF 期权波动率指数 QVIX
http://1.optbbs.com/s/vix.shtml?100ETF
:return: 深证100ETF 期权波动率指数 QVIX
:rtype: pandas.DataFrame
"""
temp_df = __get_optbbs_daily().iloc[:, [0, 75, 76, 77, 78]]
temp_df.columns = [
"date",
"open",
"high",
"low",
"close",
]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
return temp_df
def index_option_100etf_min_qvix() -> pd.DataFrame:
"""
深证100ETF 期权波动率指数 QVIX-分时
http://1.optbbs.com/s/vix.shtml?100ETF
:return: 深证100ETF 期权波动率指数 QVIX-分时
:rtype: pandas.DataFrame
"""
url = "http://1.optbbs.com/d/csv/d/vix100.csv"
temp_df = pd.read_csv(url).iloc[:, :2]
temp_df.columns = [
"time",
"qvix",
]
temp_df.loc[:, "qvix"] = pd.to_numeric(temp_df["qvix"], errors="coerce")
return temp_df
def index_option_300index_qvix() -> pd.DataFrame:
"""
中证300股指 期权波动率指数 QVIX
http://1.optbbs.com/s/vix.shtml?Index
:return: 中证300股指 期权波动率指数 QVIX
:rtype: pandas.DataFrame
"""
temp_df = __get_optbbs_daily().iloc[:, [0, 17, 18, 19, 20]]
temp_df.columns = [
"date",
"open",
"high",
"low",
"close",
]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
return temp_df
def index_option_300index_min_qvix() -> pd.DataFrame:
"""
中证300股指 期权波动率指数 QVIX-分时
http://1.optbbs.com/s/vix.shtml?Index
:return: 中证300股指 期权波动率指数 QVIX-分时
:rtype: pandas.DataFrame
"""
url = "http://1.optbbs.com/d/csv/d/vixindex.csv"
temp_df = pd.read_csv(url).iloc[:, :2]
temp_df.columns = [
"time",
"qvix",
]
temp_df["qvix"] = pd.to_numeric(temp_df["qvix"], errors="coerce")
return temp_df
def index_option_1000index_qvix() -> pd.DataFrame:
"""
中证1000股指 期权波动率指数 QVIX
http://1.optbbs.com/s/vix.shtml?Index1000
:return: 中证1000股指 期权波动率指数 QVIX
:rtype: pandas.DataFrame
"""
temp_df = __get_optbbs_daily().iloc[:, [0, 25, 26, 27, 28]]
temp_df.columns = [
"date",
"open",
"high",
"low",
"close",
]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
return temp_df
def index_option_1000index_min_qvix() -> pd.DataFrame:
"""
中证1000股指 期权波动率指数 QVIX-分时
http://1.optbbs.com/s/vix.shtml?Index1000
:return: 中证1000股指 期权波动率指数 QVIX-分时
:rtype: pandas.DataFrame
"""
url = "http://1.optbbs.com/d/csv/d/vixindex1000.csv"
temp_df = pd.read_csv(url).iloc[:, :2]
temp_df.columns = [
"time",
"qvix",
]
temp_df["qvix"] = pd.to_numeric(temp_df["qvix"], errors="coerce")
return temp_df
def index_option_50index_qvix() -> pd.DataFrame:
"""
上证50股指 期权波动率指数 QVIX
http://1.optbbs.com/s/vix.shtml?50index
:return: 上证50股指 期权波动率指数 QVIX
:rtype: pandas.DataFrame
"""
temp_df = __get_optbbs_daily().iloc[:, [0, 79, 80, 81, 82]]
temp_df.columns = [
"date",
"open",
"high",
"low",
"close",
]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
return temp_df
def index_option_50index_min_qvix() -> pd.DataFrame:
"""
上证50股指 期权波动率指数 QVIX-分时
http://1.optbbs.com/s/vix.shtml?50index
:return: 上证50股指 期权波动率指数 QVIX-分时
:rtype: pandas.DataFrame
"""
url = "http://1.optbbs.com/d/csv/d/vix50index.csv"
temp_df = pd.read_csv(url).iloc[:, :2]
temp_df.columns = [
"time",
"qvix",
]
temp_df["qvix"] = pd.to_numeric(temp_df["qvix"], errors="coerce")
return temp_df
if __name__ == "__main__":
index_option_50etf_qvix_df = index_option_50etf_qvix()
print(index_option_50etf_qvix_df)
index_option_50etf_min_qvix_df = index_option_50etf_min_qvix()
print(index_option_50etf_min_qvix_df)
index_option_300etf_qvix_df = index_option_300etf_qvix()
print(index_option_300etf_qvix_df)
index_option_300etf_min_qvix_df = index_option_300etf_min_qvix()
print(index_option_300etf_min_qvix_df)
index_option_500etf_qvix_df = index_option_500etf_qvix()
print(index_option_500etf_qvix_df)
index_option_500etf_min_qvix_df = index_option_500etf_min_qvix()
print(index_option_500etf_min_qvix_df)
index_option_cyb_qvix_df = index_option_cyb_qvix()
print(index_option_cyb_qvix_df)
index_option_cyb_min_qvix_df = index_option_cyb_min_qvix()
print(index_option_cyb_min_qvix_df)
index_option_kcb_qvix_df = index_option_kcb_qvix()
print(index_option_kcb_qvix_df)
index_option_kcb_min_qvix_df = index_option_kcb_min_qvix()
print(index_option_kcb_min_qvix_df)
index_option_100etf_qvix_df = index_option_100etf_qvix()
print(index_option_100etf_qvix_df)
index_option_100etf_min_qvix_df = index_option_100etf_min_qvix()
print(index_option_100etf_min_qvix_df)
index_option_300index_qvix_df = index_option_300index_qvix()
print(index_option_300index_qvix_df)
index_option_300index_min_qvix_df = index_option_300index_min_qvix()
print(index_option_300index_min_qvix_df)
index_option_1000index_qvix_df = index_option_1000index_qvix()
print(index_option_1000index_qvix_df)
index_option_1000index_min_qvix_df = index_option_1000index_min_qvix()
print(index_option_1000index_min_qvix_df)
index_option_50index_qvix_df = index_option_50index_qvix()
print(index_option_50index_qvix_df)
index_option_50index_min_qvix_df = index_option_50index_min_qvix()
print(index_option_50index_min_qvix_df)
@@ -0,0 +1,134 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/4/11 17:40
Desc: 申万宏源研究-申万指数-指数发布-基金指数-实时行情
https://www.swsresearch.com/institute_sw/allIndex/releasedIndex
"""
import pandas as pd
import requests
from akshare.utils.cons import headers
def index_realtime_fund_sw(symbol: str = "基础一级") -> pd.DataFrame:
"""
申万宏源研究-申万指数-指数发布-基金指数-实时行情
https://www.swsresearch.com/institute_sw/allIndex/releasedIndex
:param symbol: choice of {"基础一级", "基础二级", "基础三级", "特色指数"}
:type symbol: str
:return: 基金指数-实时行情
:rtype: pandas.DataFrame
"""
url = "https://www.swsresearch.com/insWechatSw/fundIndex/pageList"
payload = {
"pageNo": 1,
"pageSize": 50,
"indexTypeName": symbol,
"sortField": "",
"rule": "",
"indexType": 1,
}
r = requests.post(url, json=payload, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["list"])
temp_df.rename(
columns={
"swIndexCode": "指数代码",
"swIndexName": "指数名称",
"lastCloseIndex": "昨收盘",
"lastMarkup": "日涨跌幅",
"yearMarkup": "年涨跌幅",
},
inplace=True,
)
temp_df = temp_df[
[
"指数代码",
"指数名称",
"昨收盘",
"日涨跌幅",
"年涨跌幅",
]
]
temp_df["昨收盘"] = pd.to_numeric(temp_df["昨收盘"], errors="coerce")
temp_df["日涨跌幅"] = pd.to_numeric(temp_df["日涨跌幅"], errors="coerce")
temp_df["年涨跌幅"] = pd.to_numeric(temp_df["年涨跌幅"], errors="coerce")
return temp_df
def index_hist_fund_sw(symbol: str = "807200", period: str = "day") -> pd.DataFrame:
"""
申万宏源研究-申万指数-指数发布-基金指数-历史行情
https://www.swsresearch.com/institute_sw/allIndex/releasedIndex/fundDetail?code=807100
:param symbol: 基金指数代码
:type symbol: str
:param period: 周期
:type period: str
:return: 历史行情
:rtype: pandas.DataFrame
"""
period_map = {
"day": "DAY",
"week": "WEEK",
"month": "MONTH",
}
url = "https://www.swsresearch.com/insWechatSw/fundIndex/getFundKChartData"
payload = {"swIndexCode": symbol, "type": period_map[period]}
r = requests.post(url, json=payload, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.rename(
columns={
"bargaindate": "日期",
"swIndexName": "指数名称",
"swindexcode": "指数代码",
"closeindex": "收盘指数",
"maxindex": "最高指数",
"minindex": "最低指数",
"openindex": "开盘指数",
"markup": "涨跌幅",
},
inplace=True,
)
temp_df = temp_df[
[
"日期",
"收盘指数",
"开盘指数",
"最高指数",
"最低指数",
"涨跌幅",
]
]
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["收盘指数"] = pd.to_numeric(temp_df["收盘指数"], errors="coerce")
temp_df["最高指数"] = pd.to_numeric(temp_df["最高指数"], errors="coerce")
temp_df["最低指数"] = pd.to_numeric(temp_df["最低指数"], errors="coerce")
temp_df["开盘指数"] = pd.to_numeric(temp_df["开盘指数"], errors="coerce")
temp_df["涨跌幅"] = pd.to_numeric(temp_df["涨跌幅"], errors="coerce")
return temp_df
if __name__ == "__main__":
index_realtime_fund_sw_df = index_realtime_fund_sw(symbol="基础一级")
print(index_realtime_fund_sw_df)
index_realtime_fund_sw_df = index_realtime_fund_sw(symbol="基础二级")
print(index_realtime_fund_sw_df)
index_realtime_fund_sw_df = index_realtime_fund_sw(symbol="基础三级")
print(index_realtime_fund_sw_df)
index_realtime_fund_sw_df = index_realtime_fund_sw(symbol="特色指数")
print(index_realtime_fund_sw_df)
index_hist_fund_sw_df = index_hist_fund_sw(symbol="807200", period="day")
print(index_hist_fund_sw_df)
index_hist_fund_sw_df = index_hist_fund_sw(symbol="807200", period="week")
print(index_hist_fund_sw_df)
index_hist_fund_sw_df = index_hist_fund_sw(symbol="807200", period="month")
print(index_hist_fund_sw_df)
@@ -0,0 +1,567 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/10/23 13:00
Desc: 申万宏源研究-指数系列
https://www.swsresearch.com/institute_sw/allIndex/releasedIndex
"""
import math
import pandas as pd
import requests
from akshare.utils.tqdm import get_tqdm
def index_hist_sw(symbol: str = "801030", period: str = "day") -> pd.DataFrame:
"""
申万宏源研究-指数发布-指数详情-指数历史数据
https://www.swsresearch.com/institute_sw/allIndex/releasedIndex/releasedetail?code=801001&name=%E7%94%B3%E4%B8%8750
:param symbol: 指数代码
:type symbol: str
:param period: choice of {"day", "week", "month"}
:type period: str
:return: 指数历史数据
:rtype: pandas.DataFrame
"""
period_map = {
"day": "DAY",
"week": "WEEK",
"month": "MONTH",
}
url = "https://www.swsresearch.com/institute-sw/api/index_publish/trend/"
params = {
"swindexcode": symbol,
"period": period_map[period],
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/114.0.0.0 Safari/537.36",
}
r = requests.get(url, params=params, headers=headers, verify=False)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.rename(
columns={
"swindexcode": "代码",
"bargaindate": "日期",
"openindex": "开盘",
"maxindex": "最高",
"minindex": "最低",
"closeindex": "收盘",
"hike": "",
"markup": "",
"bargainamount": "成交量",
"bargainsum": "成交额",
},
inplace=True,
)
temp_df = temp_df[
[
"代码",
"日期",
"收盘",
"开盘",
"最高",
"最低",
"成交量",
"成交额",
]
]
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["收盘"] = pd.to_numeric(temp_df["收盘"], errors="coerce")
temp_df["最高"] = pd.to_numeric(temp_df["最高"], errors="coerce")
temp_df["最低"] = pd.to_numeric(temp_df["最低"], errors="coerce")
temp_df["成交量"] = pd.to_numeric(temp_df["成交量"], errors="coerce")
temp_df["成交额"] = pd.to_numeric(temp_df["成交额"], errors="coerce")
return temp_df
def index_min_sw(symbol: str = "801001") -> pd.DataFrame:
"""
申万宏源研究-指数发布-指数详情-指数分时数据
https://www.swsresearch.com/institute_sw/allIndex/releasedIndex/releasedetail?code=801001&name=%E7%94%B3%E4%B8%8750
:param symbol: 指数代码
:type symbol: str
:return: 指数分时数据
:rtype: pandas.DataFrame
"""
url = (
"https://www.swsresearch.com/institute-sw/api/index_publish/details/timelines/"
)
params = {
"swindexcode": symbol,
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/114.0.0.0 Safari/537.36"
}
r = requests.get(url, params=params, headers=headers, verify=False)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.rename(
columns={
"l1": "代码",
"l2": "名称",
"l8": "价格",
"trading_date": "日期",
"trading_time": "时间",
},
inplace=True,
)
temp_df = temp_df[
[
"代码",
"名称",
"价格",
"日期",
"时间",
]
]
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["价格"] = pd.to_numeric(temp_df["价格"], errors="coerce")
return temp_df
def index_component_sw(symbol: str = "801001") -> pd.DataFrame:
"""
申万宏源研究-指数发布-指数详情-成分股
https://www.swsresearch.com/institute_sw/allIndex/releasedIndex/releasedetail?code=801001&name=%E7%94%B3%E4%B8%8750
:param symbol: 指数代码
:type symbol: str
:return: 成分股
:rtype: pandas.DataFrame
"""
url = "https://www.swsresearch.com/institute-sw/api/index_publish/details/component_stocks/"
params = {"swindexcode": symbol, "page": "1", "page_size": "10000"}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/114.0.0.0 Safari/537.36"
}
r = requests.get(url, params=params, headers=headers, verify=False)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["results"])
temp_df.reset_index(inplace=True)
temp_df["index"] = temp_df["index"] + 1
temp_df.rename(
columns={
"index": "序号",
"stockcode": "证券代码",
"stockname": "证券名称",
"newweight": "最新权重",
"beginningdate": "计入日期",
},
inplace=True,
)
temp_df = temp_df[
[
"序号",
"证券代码",
"证券名称",
"最新权重",
"计入日期",
]
]
temp_df["计入日期"] = pd.to_datetime(temp_df["计入日期"], errors="coerce").dt.date
temp_df["最新权重"] = pd.to_numeric(temp_df["最新权重"], errors="coerce")
return temp_df
def __index_realtime_sw(symbol: str = "大类风格指数") -> pd.DataFrame:
"""
申万宏源研究-申万指数-股票指数
https://www.swsresearch.com/institute_sw/allIndex/releasedIndex
:param symbol: choice of {"大类风格指数", "金创指数"}
:type symbol: str
:return: 指数系列实时行情数据
:rtype: pandas.DataFrame
"""
url = "https://www.swsresearch.com/insWechatSw/dflgOrJcIndex/pageList"
payload = {
"pageNo": 1,
"pageSize": 10,
"indexTypeName": symbol,
"sortField": "",
"rule": "",
"indexType": 1,
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/114.0.0.0 Safari/537.36"
}
r = requests.post(url, json=payload, headers=headers, verify=False)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["list"])
temp_df.rename(
columns={
"swIndexCode": "指数代码",
"swIndexName": "指数名称",
"lastCloseIndex": "昨收盘",
"lastMarkup": "日涨跌幅",
"yearMarkup": "年涨跌幅",
},
inplace=True,
)
temp_df = temp_df[
[
"指数代码",
"指数名称",
"昨收盘",
"日涨跌幅",
"年涨跌幅",
]
]
temp_df["昨收盘"] = pd.to_numeric(temp_df["昨收盘"], errors="coerce")
temp_df["日涨跌幅"] = pd.to_numeric(temp_df["日涨跌幅"], errors="coerce")
temp_df["年涨跌幅"] = pd.to_numeric(temp_df["年涨跌幅"], errors="coerce")
return temp_df
def index_realtime_sw(symbol: str = "二级行业") -> pd.DataFrame:
"""
申万宏源研究-指数系列
https://www.swsresearch.com/institute_sw/allIndex/releasedIndex
:param symbol: choice of {"市场表征", "一级行业", "二级行业", "风格指数", "大类风格指数", "金创指数"}
:type symbol: str
:return: 指数系列实时行情数据
:rtype: pandas.DataFrame
"""
if symbol in {"大类风格指数", "金创指数"}:
temp_df = __index_realtime_sw(symbol)
return temp_df
url = "https://www.swsresearch.com/institute-sw/api/index_publish/current/"
params = {"page": "1", "page_size": "50", "indextype": symbol}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/114.0.0.0 Safari/537.36"
}
r = requests.get(url, params=params, headers=headers, verify=False)
data_json = r.json()
total_num = data_json["data"]["count"]
total_page = math.ceil(total_num / 50)
big_df = pd.DataFrame()
tqdm = get_tqdm()
for page in tqdm(range(1, total_page + 1), leave=False):
params.update({"page": page})
r = requests.get(url, params=params, headers=headers, verify=False)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["results"])
big_df = pd.concat(objs=[big_df, temp_df], ignore_index=True)
big_df.columns = [
"指数代码",
"指数名称",
"昨收盘",
"今开盘",
"成交额",
"最高价",
"最低价",
"最新价",
"成交量",
]
big_df = big_df[
[
"指数代码",
"指数名称",
"昨收盘",
"今开盘",
"最新价",
"成交额",
"成交量",
"最高价",
"最低价",
]
]
big_df["昨收盘"] = pd.to_numeric(big_df["昨收盘"], errors="coerce")
big_df["今开盘"] = pd.to_numeric(big_df["今开盘"], errors="coerce")
big_df["最新价"] = pd.to_numeric(big_df["最新价"], errors="coerce")
big_df["成交额"] = pd.to_numeric(big_df["成交额"], errors="coerce")
big_df["成交量"] = pd.to_numeric(big_df["成交量"], errors="coerce")
big_df["最高价"] = pd.to_numeric(big_df["最高价"], errors="coerce")
big_df["最低价"] = pd.to_numeric(big_df["最低价"], errors="coerce")
return big_df
def index_analysis_daily_sw(
symbol: str = "市场表征",
start_date: str = "20221103",
end_date: str = "20221103",
) -> pd.DataFrame:
"""
申万宏源研究-指数分析
https://www.swsresearch.com/institute_sw/allIndex/analysisIndex
:param symbol: choice of {"市场表征", "一级行业", "二级行业", "风格指数"}
:type symbol: str
:param start_date: 开始日期
:type start_date: str
:param end_date: 结束日期
:type end_date: str
:return: 指数分析
:rtype: pandas.DataFrame
"""
url = "https://www.swsresearch.com/institute-sw/api/index_analysis/index_analysis_report/"
params = {
"page": "1",
"page_size": "50",
"index_type": symbol,
"start_date": "-".join([start_date[:4], start_date[4:6], start_date[6:]]),
"end_date": "-".join([end_date[:4], end_date[4:6], end_date[6:]]),
"type": "DAY",
"swindexcode": "all",
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/114.0.0.0 Safari/537.36"
}
r = requests.get(url, params=params, headers=headers, verify=False)
data_json = r.json()
total_num = data_json["data"]["count"]
total_page = math.ceil(total_num / 50)
big_df = pd.DataFrame()
tqdm = get_tqdm()
for page in tqdm(range(1, total_page + 1), leave=False):
params.update({"page": page})
r = requests.get(url, params=params, headers=headers, verify=False)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["results"])
big_df = pd.concat(objs=[big_df, temp_df], ignore_index=True)
big_df.rename(
columns={
"swindexcode": "指数代码",
"swindexname": "指数名称",
"bargaindate": "发布日期",
"closeindex": "收盘指数",
"bargainamount": "成交量",
"markup": "涨跌幅",
"turnoverrate": "换手率",
"pe": "市盈率",
"pb": "市净率",
"meanprice": "均价",
"bargainsumrate": "成交额占比",
"negotiablessharesum1": "流通市值",
"negotiablessharesum2": "平均流通市值",
"dp": "股息率",
},
inplace=True,
)
big_df["发布日期"] = pd.to_datetime(big_df["发布日期"], errors="coerce").dt.date
big_df["收盘指数"] = pd.to_numeric(big_df["收盘指数"], errors="coerce")
big_df["成交量"] = pd.to_numeric(big_df["成交量"], errors="coerce")
big_df["涨跌幅"] = pd.to_numeric(big_df["涨跌幅"], errors="coerce")
big_df["换手率"] = pd.to_numeric(big_df["换手率"], errors="coerce")
big_df["市盈率"] = pd.to_numeric(big_df["市盈率"], errors="coerce")
big_df["市净率"] = pd.to_numeric(big_df["市净率"], errors="coerce")
big_df["均价"] = pd.to_numeric(big_df["均价"], errors="coerce")
big_df["成交额占比"] = pd.to_numeric(big_df["成交额占比"], errors="coerce")
big_df["流通市值"] = pd.to_numeric(big_df["流通市值"], errors="coerce")
big_df["平均流通市值"] = pd.to_numeric(big_df["平均流通市值"], errors="coerce")
big_df["股息率"] = pd.to_numeric(big_df["股息率"], errors="coerce")
big_df.sort_values(by=["发布日期"], inplace=True, ignore_index=True)
return big_df
def index_analysis_week_month_sw(symbol: str = "month") -> pd.DataFrame:
"""
申万宏源研究-/月报表-日期序列
https://www.swsresearch.com/institute_sw/allIndex/analysisIndex
:param symbol: choice of {"week", "month"}
:type symbol: str
:return: 日期序列
:rtype: pandas.DataFrame
"""
url = "https://www.swsresearch.com/institute-sw/api/index_analysis/week_month_datetime/"
params = {"type": symbol.upper()}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/114.0.0.0 Safari/537.36"
}
r = requests.get(url, params=params, headers=headers, verify=False)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df["bargaindate"] = pd.to_datetime(
temp_df["bargaindate"], errors="coerce"
).dt.date
temp_df.columns = ["date"]
temp_df.sort_values(by=["date"], inplace=True, ignore_index=True)
return temp_df
def index_analysis_weekly_sw(
symbol: str = "市场表征",
date: str = "20221104",
) -> pd.DataFrame:
"""
申万宏源研究-指数分析-周报告
https://www.swsresearch.com/institute_sw/allIndex/analysisIndex
:param symbol: choice of {"市场表征", "一级行业", "二级行业", "风格指数"}
:type symbol: str
:param date: 查询日期; 通过调用 ak.index_analysis_week_month_sw(date="20221104") 接口获取
:type date: str
:return: 指数分析
:rtype: pandas.DataFrame
"""
url = "https://www.swsresearch.com/institute-sw/api/index_analysis/index_analysis_reports/"
params = {
"page": "1",
"page_size": "50",
"index_type": symbol,
"bargaindate": "-".join([date[:4], date[4:6], date[6:]]),
"type": "WEEK",
"swindexcode": "all",
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/114.0.0.0 Safari/537.36"
}
r = requests.get(url, params=params, headers=headers, verify=False)
data_json = r.json()
total_num = data_json["data"]["count"]
total_page = math.ceil(total_num / 50)
big_df = pd.DataFrame()
tqdm = get_tqdm()
for page in tqdm(range(1, total_page + 1), leave=False):
params.update({"page": page})
r = requests.get(url, params=params, headers=headers, verify=False)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["results"])
big_df = pd.concat(objs=[big_df, temp_df], ignore_index=True)
big_df.rename(
columns={
"swindexcode": "指数代码",
"swindexname": "指数名称",
"bargaindate": "发布日期",
"closeindex": "收盘指数",
"bargainamount": "成交量",
"markup": "涨跌幅",
"turnoverrate": "换手率",
"pe": "市盈率",
"pb": "市净率",
"meanprice": "均价",
"bargainsumrate": "成交额占比",
"negotiablessharesum1": "流通市值",
"negotiablessharesum2": "平均流通市值",
"dp": "股息率",
},
inplace=True,
)
big_df["发布日期"] = pd.to_datetime(big_df["发布日期"], errors="coerce").dt.date
big_df["收盘指数"] = pd.to_numeric(big_df["收盘指数"], errors="coerce")
big_df["成交量"] = pd.to_numeric(big_df["成交量"], errors="coerce")
big_df["涨跌幅"] = pd.to_numeric(big_df["涨跌幅"], errors="coerce")
big_df["换手率"] = pd.to_numeric(big_df["换手率"], errors="coerce")
big_df["市盈率"] = pd.to_numeric(big_df["市盈率"], errors="coerce")
big_df["市净率"] = pd.to_numeric(big_df["市净率"], errors="coerce")
big_df["均价"] = pd.to_numeric(big_df["均价"], errors="coerce")
big_df["成交额占比"] = pd.to_numeric(big_df["成交额占比"], errors="coerce")
big_df["流通市值"] = pd.to_numeric(big_df["流通市值"], errors="coerce")
big_df["平均流通市值"] = pd.to_numeric(big_df["平均流通市值"], errors="coerce")
big_df["股息率"] = pd.to_numeric(big_df["股息率"], errors="coerce")
big_df.sort_values(by=["发布日期"], inplace=True, ignore_index=True)
return big_df
def index_analysis_monthly_sw(
symbol: str = "市场表征",
date: str = "20221031",
) -> pd.DataFrame:
"""
申万宏源研究-指数分析-月报告
https://www.swsresearch.com/institute_sw/allIndex/analysisIndex
:param symbol: choice of {"市场表征", "一级行业", "二级行业", "风格指数"}
:type symbol: str
:param date: 查询日期; 通过调用 ak.index_analysis_week_month_sw() 接口获取
:type date: str
:return: 指数分析
:rtype: pandas.DataFrame
"""
url = "https://www.swsresearch.com/institute-sw/api/index_analysis/index_analysis_reports/"
params = {
"page": "1",
"page_size": "50",
"index_type": symbol,
"bargaindate": "-".join([date[:4], date[4:6], date[6:]]),
"type": "MONTH",
"swindexcode": "all",
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/114.0.0.0 Safari/537.36"
}
r = requests.get(url, params=params, headers=headers, verify=False)
data_json = r.json()
total_num = data_json["data"]["count"]
total_page = math.ceil(total_num / 50)
big_df = pd.DataFrame()
tqdm = get_tqdm()
for page in tqdm(range(1, total_page + 1), leave=False):
params.update({"page": page})
r = requests.get(url, params=params, headers=headers, verify=False)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["results"])
big_df = pd.concat(objs=[big_df, temp_df], ignore_index=True)
big_df.rename(
columns={
"swindexcode": "指数代码",
"swindexname": "指数名称",
"bargaindate": "发布日期",
"closeindex": "收盘指数",
"bargainamount": "成交量",
"markup": "涨跌幅",
"turnoverrate": "换手率",
"pe": "市盈率",
"pb": "市净率",
"meanprice": "均价",
"bargainsumrate": "成交额占比",
"negotiablessharesum1": "流通市值",
"negotiablessharesum2": "平均流通市值",
"dp": "股息率",
},
inplace=True,
)
big_df["发布日期"] = pd.to_datetime(big_df["发布日期"], errors="coerce").dt.date
big_df["收盘指数"] = pd.to_numeric(big_df["收盘指数"], errors="coerce")
big_df["成交量"] = pd.to_numeric(big_df["成交量"], errors="coerce")
big_df["涨跌幅"] = pd.to_numeric(big_df["涨跌幅"], errors="coerce")
big_df["换手率"] = pd.to_numeric(big_df["换手率"], errors="coerce")
big_df["市盈率"] = pd.to_numeric(big_df["市盈率"], errors="coerce")
big_df["市净率"] = pd.to_numeric(big_df["市净率"], errors="coerce")
big_df["均价"] = pd.to_numeric(big_df["均价"], errors="coerce")
big_df["成交额占比"] = pd.to_numeric(big_df["成交额占比"], errors="coerce")
big_df["流通市值"] = pd.to_numeric(big_df["流通市值"], errors="coerce")
big_df["平均流通市值"] = pd.to_numeric(big_df["平均流通市值"], errors="coerce")
big_df["股息率"] = pd.to_numeric(big_df["股息率"], errors="coerce")
big_df.sort_values(by=["发布日期"], inplace=True, ignore_index=True)
return big_df
if __name__ == "__main__":
index_hist_sw_df = index_hist_sw(symbol="801193", period="day")
print(index_hist_sw_df)
index_min_sw_df = index_min_sw(symbol="801001")
print(index_min_sw_df)
index_component_sw_df = index_component_sw(symbol="801001")
print(index_component_sw_df)
index_realtime_sw_df = index_realtime_sw(symbol="市场表征")
print(index_realtime_sw_df)
index_analysis_daily_sw_df = index_analysis_daily_sw(
symbol="市场表征", start_date="20241025", end_date="20241025"
)
print(index_analysis_daily_sw_df)
index_analysis_week_month_sw_df = index_analysis_week_month_sw(symbol="month")
print(index_analysis_week_month_sw_df)
index_analysis_weekly_sw_df = index_analysis_weekly_sw(
symbol="市场表征", date="20241025"
)
print(index_analysis_weekly_sw_df)
index_analysis_monthly_sw_df = index_analysis_monthly_sw(
symbol="市场表征", date="20240930"
)
print(index_analysis_monthly_sw_df)
@@ -0,0 +1,47 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/12/26 18:30
Desc: 商品现货价格指数
https://finance.sina.com.cn/futuremarket/spotprice.shtml#titlePos_0
"""
import pandas as pd
import requests
def spot_goods(symbol: str = "波罗的海干散货指数") -> pd.DataFrame:
"""
新浪财经-商品现货价格指数
https://finance.sina.com.cn/futuremarket/spotprice.shtml#titlePos_0
:param symbol: choice of {"波罗的海干散货指数", "钢坯价格指数", "澳大利亚粉矿价格"}
:type symbol: str
:return: 商品现货价格指数
:rtype: pandas.DataFrame
"""
url = "https://stock.finance.sina.com.cn/futures/api/openapi.php/GoodsIndexService.get_goods_index"
symbol_url_dict = {
"波罗的海干散货指数": "BDI",
"钢坯价格指数": "GP",
"澳大利亚粉矿价格": "PB",
}
params = {"symbol": symbol_url_dict[symbol], "table": "0"}
r = requests.get(url, params=params)
r.encoding = "gbk"
data_json = r.json()
temp_df = pd.DataFrame(data_json["result"]["data"]["data"])
temp_df = temp_df[["opendate", "price", "zde", "zdf"]]
temp_df.columns = ["日期", "指数", "涨跌额", "涨跌幅"]
temp_df["日期"] = pd.to_datetime(
temp_df["日期"], format="%Y-%m-%d", errors="coerce"
).dt.date
temp_df["指数"] = pd.to_numeric(temp_df["指数"], errors="coerce")
temp_df["涨跌额"] = pd.to_numeric(temp_df["涨跌额"], errors="coerce")
temp_df["涨跌幅"] = pd.to_numeric(temp_df["涨跌幅"], errors="coerce")
temp_df.dropna(inplace=True, ignore_index=True)
return temp_df
if __name__ == "__main__":
spot_goods_df = spot_goods(symbol="波罗的海干散货指数")
print(spot_goods_df)
@@ -0,0 +1,302 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2025/3/15 22:30
Desc: 港股股票指数数据-新浪-东财
所有指数-实时行情数据和历史行情数据
https://finance.sina.com.cn/realstock/company/sz399552/nc.shtml
https://quote.eastmoney.com/gb/zsHSTECF2L.html
"""
import re
import pandas as pd
import requests
import py_mini_racer
from functools import lru_cache
from akshare.stock.cons import hk_js_decode
from akshare.utils.func import fetch_paginated_data
def _replace_comma(x) -> str:
"""
去除单元格中的 ","
:param x: 单元格元素
:type x: str
:return: 处理后的值或原值
:rtype: str
"""
if "," in str(x):
return str(x).replace(",", "")
else:
return x
def get_hk_index_page_count() -> int:
"""
指数的总页数
https://vip.stock.finance.sina.com.cn/mkt/#zs_hk
:return: 需要抓取的指数的总页数
:rtype: int
"""
res = requests.get(
"https://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getNameCount?node=zs_hk"
)
page_count = int(re.findall(re.compile(r"\d+"), res.text)[0]) / 80
if isinstance(page_count, int):
return page_count
else:
return int(page_count) + 1
def stock_hk_index_spot_sina() -> pd.DataFrame:
"""
新浪财经-行情中心-港股指数
大量采集会被目标网站服务器封禁 IP, 如果被封禁 IP, 10 分钟后再试
https://vip.stock.finance.sina.com.cn/mkt/#zs_hk
:return: 所有指数的实时行情数据
:rtype: pandas.DataFrame
"""
url = (
"https://hq.sinajs.cn/rn=mtf2t&list=hkCES100,hkCES120,hkCES280,hkCES300,hkCESA80,hkCESG10,"
"hkCESHKM,hkCSCMC,hkCSHK100,hkCSHKDIV,hkCSHKLC,hkCSHKLRE,hkCSHKMCS,hkCSHKME,hkCSHKPE,hkCSHKSE,"
"hkCSI300,hkCSRHK50,hkGEM,hkHKL,hkHSCCI,hkHSCEI,hkHSI,hkHSMBI,hkHSMOGI,hkHSMPI,hkHSTECH,hkSSE180,"
"hkSSE180GV,hkSSE380,hkSSE50,hkSSECEQT,hkSSECOMP,hkSSEDIV,hkSSEITOP,hkSSEMCAP,hkSSEMEGA,hkVHSI"
)
headers = {"Referer": "https://vip.stock.finance.sina.com.cn/"}
r = requests.get(url, headers=headers)
data_text = r.text
data_list = [
item.split('"')[1].split(",")
for item in data_text.split("\n")
if len(item.split('"')) > 1
]
temp_df = pd.DataFrame(data_list)
temp_df.columns = [
"代码",
"名称",
"今开",
"昨收",
"最高",
"最低",
"最新价",
"涨跌额",
"涨跌幅",
"_",
"_",
"_",
"_",
"_",
"_",
"_",
"_",
"_",
"_",
]
temp_df = temp_df[
[
"代码",
"名称",
"最新价",
"涨跌额",
"涨跌幅",
"昨收",
"今开",
"最高",
"最低",
]
]
temp_df["最新价"] = pd.to_numeric(temp_df["最新价"], errors="coerce")
temp_df["涨跌额"] = pd.to_numeric(temp_df["涨跌额"], errors="coerce")
temp_df["涨跌幅"] = pd.to_numeric(temp_df["涨跌幅"], errors="coerce")
temp_df["昨收"] = pd.to_numeric(temp_df["昨收"], errors="coerce")
temp_df["今开"] = pd.to_numeric(temp_df["今开"], errors="coerce")
temp_df["最高"] = pd.to_numeric(temp_df["最高"], errors="coerce")
temp_df["最低"] = pd.to_numeric(temp_df["最低"], errors="coerce")
return temp_df
def stock_hk_index_daily_sina(symbol: str = "CES100") -> pd.DataFrame:
"""
新浪财经-港股指数-历史行情数据
https://stock.finance.sina.com.cn/hkstock/quotes/CES100.html
:param symbol: CES100, 港股指数代码
:type symbol: str
:return: 历史行情数据
:rtype: pandas.DataFrame
"""
url = f"https://finance.sina.com.cn/stock/hkstock/{symbol}/klc2_kl.js"
params = {"d": "2023_5_01"}
res = requests.get(url, params=params)
js_code = py_mini_racer.MiniRacer()
js_code.eval(hk_js_decode)
dict_list = js_code.call(
"d", res.text.split("=")[1].split(";")[0].replace('"', "")
) # 执行js解密代码
temp_df = pd.DataFrame(dict_list)
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["volume"] = pd.to_numeric(temp_df["volume"], errors="coerce")
return temp_df
def stock_hk_index_spot_em() -> pd.DataFrame:
"""
东方财富网-行情中心-港股-指数实时行情
https://quote.eastmoney.com/center/gridlist.html#hk_index
:return: 指数行情
:rtype: pandas.DataFrame
"""
url = "https://15.push2.eastmoney.com/api/qt/clist/get"
params = {
"pn": "1",
"pz": "100",
"po": "1",
"np": "1",
"ut": "bd1d9ddb04089700cf9c27f6f7426281",
"fltt": "2",
"invt": "2",
"wbp2u": "|0|0|0|web",
"fid": "f3",
"fs": "m:124,m:125,m:305",
"fields": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,"
"f26,f22,f33,f11,f62,f128,f136,f115,f152",
}
temp_df = fetch_paginated_data(url, params)
temp_df.rename(
columns={
"index": "序号",
"f2": "最新价",
"f3": "涨跌幅",
"f4": "涨跌额",
"f5": "成交量",
"f6": "成交额",
"f12": "代码",
"f13": "内部编号",
"f14": "名称",
"f15": "最高",
"f16": "最低",
"f17": "今开",
"f18": "昨收",
},
inplace=True,
)
temp_df = temp_df[
[
"序号",
"内部编号",
"代码",
"名称",
"最新价",
"涨跌额",
"涨跌幅",
"今开",
"最高",
"最低",
"昨收",
"成交量",
"成交额",
]
]
temp_df["最新价"] = pd.to_numeric(temp_df["最新价"], errors="coerce")
temp_df["涨跌额"] = pd.to_numeric(temp_df["涨跌额"], errors="coerce")
temp_df["涨跌幅"] = pd.to_numeric(temp_df["涨跌幅"], errors="coerce")
temp_df["今开"] = pd.to_numeric(temp_df["今开"], errors="coerce")
temp_df["最高"] = pd.to_numeric(temp_df["最高"], errors="coerce")
temp_df["最低"] = pd.to_numeric(temp_df["最低"], errors="coerce")
temp_df["昨收"] = pd.to_numeric(temp_df["昨收"], errors="coerce")
temp_df["成交量"] = pd.to_numeric(temp_df["成交量"], errors="coerce")
temp_df["成交额"] = pd.to_numeric(temp_df["成交额"], errors="coerce")
return temp_df
@lru_cache()
def _symbol_code_dict() -> dict:
"""
缓存 ak.stock_hk_index_spot_em() 接口中的代码与内部编号
https://quote.eastmoney.com/center/gridlist.html#hk_index
:return: 代码与内部编号
:rtype: dict
"""
__stock_hk_index_spot_em_df = stock_hk_index_spot_em()
symbol_code_dict = dict(
zip(
__stock_hk_index_spot_em_df["代码"], __stock_hk_index_spot_em_df["内部编号"]
)
)
return symbol_code_dict
def stock_hk_index_daily_em(symbol: str = "HSTECF2L") -> pd.DataFrame:
"""
东方财富网-港股-股票指数数据
https://quote.eastmoney.com/gb/zsHSTECF2L.html
:param symbol: 港股指数代码; 可以通过 ak.stock_hk_index_spot_em() 获取
:type symbol: str
:return: 指数数据
:rtype: pandas.DataFrame
"""
symbol_code_dict = _symbol_code_dict()
symbol_code_dict.update(
{
"HSAHP": "100",
}
)
symbol_str = f"{symbol_code_dict[symbol]}.{symbol}"
url = "https://push2his.eastmoney.com/api/qt/stock/kline/get"
params = {
"secid": symbol_str,
"klt": "101", # 日频率
"fqt": "1",
"lmt": "10000",
"end": "20500000",
"iscca": "1",
"fields1": "f1,f2,f3,f4,f5,f6,f7,f8",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61,f62,f63,f64",
"ut": "f057cbcbce2a86e2866ab8877db1d059",
"forcect": "1",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame([item.split(",") for item in data_json["data"]["klines"]])
temp_df.columns = [
"date",
"open",
"latest",
"high",
"low",
"-",
"-",
"-",
"-",
"-",
"-",
"-",
"-",
"-",
]
temp_df = temp_df[["date", "open", "high", "low", "latest"]]
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["latest"] = pd.to_numeric(temp_df["latest"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
return temp_df
if __name__ == "__main__":
stock_hk_index_spot_sina_df = stock_hk_index_spot_sina()
print(stock_hk_index_spot_sina_df)
stock_hk_index_daily_sina_df = stock_hk_index_daily_sina(symbol="CES100")
print(stock_hk_index_daily_sina_df)
stock_hk_index_spot_em_df = stock_hk_index_spot_em()
print(stock_hk_index_spot_em_df)
stock_hk_index_daily_em_df = stock_hk_index_daily_em(symbol="HSTECH")
print(stock_hk_index_daily_em_df)
@@ -0,0 +1,45 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/3/17 18:20
Desc: 新浪财经-美股指数行情
https://stock.finance.sina.com.cn/usstock/quotes/.IXIC.html
"""
import pandas as pd
import requests
import py_mini_racer
from akshare.stock.cons import (
zh_js_decode,
)
def index_us_stock_sina(symbol: str = ".INX") -> pd.DataFrame:
"""
新浪财经-美股指数行情
https://stock.finance.sina.com.cn/usstock/quotes/.IXIC.html
:param symbol: choice of {".IXIC", ".DJI", ".INX", ".NDX"}
:type symbol: str
:return: 美股指数行情
:rtype: pandas.DataFrame
"""
url = f"https://finance.sina.com.cn/staticdata/us/{symbol}"
r = requests.get(url)
js_code = py_mini_racer.MiniRacer()
js_code.eval(zh_js_decode)
dict_list = js_code.call("d", r.text.split("=")[1].split(";")[0].replace('"', ""))
temp_df = pd.DataFrame(dict_list)
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
temp_df["volume"] = pd.to_numeric(temp_df["volume"], errors="coerce")
temp_df["amount"] = pd.to_numeric(temp_df["amount"], errors="coerce")
return temp_df
if __name__ == "__main__":
index_us_stock_sina_df = index_us_stock_sina(symbol=".INX")
print(index_us_stock_sina_df)
@@ -0,0 +1,508 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2026/5/2 16:30
Desc: 股票指数数据-新浪-东财-腾讯
所有指数-实时行情数据和历史行情数据
https://finance.sina.com.cn/realstock/company/sz399552/nc.shtml
"""
import datetime
import re
import pandas as pd
import py_mini_racer
import requests
from akshare.index.cons import (
zh_sina_index_stock_payload,
zh_sina_index_stock_url,
zh_sina_index_stock_count_url,
zh_sina_index_stock_hist_url,
)
from akshare.stock.cons import hk_js_decode
from akshare.utils import demjson
from akshare.utils.func import fetch_paginated_data
from akshare.utils.tqdm import get_tqdm
def _replace_comma(x):
"""
去除单元格中的 ","
:param x: 单元格元素
:type x: str
:return: 处理后的值或原值
:rtype: str
"""
if "," in str(x):
return str(x).replace(",", "")
else:
return x
def get_zh_index_page_count() -> int:
"""
指数的总页数
https://vip.stock.finance.sina.com.cn/mkt/#hs_s
:return: 需要抓取的指数的总页数
:rtype: int
"""
res = requests.get(zh_sina_index_stock_count_url)
page_count = int(re.findall(re.compile(r"\d+"), res.text)[0]) / 80
if isinstance(page_count, int):
return page_count
else:
return int(page_count) + 1
def stock_zh_index_spot_sina() -> pd.DataFrame:
"""
新浪财经-行情中心首页-A股-分类-所有指数
大量采集会被目标网站服务器封禁 IP, 如果被封禁 IP, 10 分钟后再试
https://vip.stock.finance.sina.com.cn/mkt/#hs_s
:return: 所有指数的实时行情数据
:rtype: pandas.DataFrame
"""
big_df = pd.DataFrame()
page_count = get_zh_index_page_count()
zh_sina_stock_payload_copy = zh_sina_index_stock_payload.copy()
tqdm = get_tqdm()
for page in tqdm(range(1, page_count + 1), leave=False):
zh_sina_stock_payload_copy.update({"page": page})
res = requests.get(zh_sina_index_stock_url, params=zh_sina_stock_payload_copy)
data_json = demjson.decode(res.text)
big_df = pd.concat(objs=[big_df, pd.DataFrame(data_json)], ignore_index=True)
big_df = big_df.map(_replace_comma)
big_df["trade"] = pd.to_numeric(big_df["trade"], errors="coerce")
big_df["pricechange"] = pd.to_numeric(big_df["pricechange"], errors="coerce")
big_df["changepercent"] = pd.to_numeric(big_df["changepercent"], errors="coerce")
big_df["buy"] = pd.to_numeric(big_df["buy"], errors="coerce")
big_df["sell"] = pd.to_numeric(big_df["sell"], errors="coerce")
big_df["settlement"] = pd.to_numeric(big_df["settlement"], errors="coerce")
big_df["open"] = pd.to_numeric(big_df["open"], errors="coerce")
big_df["high"] = pd.to_numeric(big_df["high"], errors="coerce")
big_df["low"] = pd.to_numeric(big_df["low"], errors="coerce")
big_df.columns = [
"代码",
"名称",
"最新价",
"涨跌额",
"涨跌幅",
"_",
"_",
"昨收",
"今开",
"最高",
"最低",
"成交量",
"成交额",
"_",
"_",
]
big_df = big_df[
[
"代码",
"名称",
"最新价",
"涨跌额",
"涨跌幅",
"昨收",
"今开",
"最高",
"最低",
"成交量",
"成交额",
]
]
big_df["最新价"] = pd.to_numeric(big_df["最新价"], errors="coerce")
big_df["涨跌额"] = pd.to_numeric(big_df["涨跌额"], errors="coerce")
big_df["涨跌幅"] = pd.to_numeric(big_df["涨跌幅"], errors="coerce")
big_df["昨收"] = pd.to_numeric(big_df["昨收"], errors="coerce")
big_df["今开"] = pd.to_numeric(big_df["今开"], errors="coerce")
big_df["最高"] = pd.to_numeric(big_df["最高"], errors="coerce")
big_df["最低"] = pd.to_numeric(big_df["最低"], errors="coerce")
big_df["成交量"] = pd.to_numeric(big_df["成交量"], errors="coerce")
big_df["成交额"] = pd.to_numeric(big_df["成交额"], errors="coerce")
return big_df
def __stock_zh_main_spot_em() -> pd.DataFrame:
"""
东方财富网-行情中心-沪深重要指数
https://quote.eastmoney.com/center/hszs.html
:return: 指数的实时行情数据
:rtype: pandas.DataFrame
"""
url = "https://33.push2.eastmoney.com/api/qt/clist/get"
params = {
"pn": "1",
"pz": "100",
"po": "1",
"np": "1",
"ut": "bd1d9ddb04089700cf9c27f6f7426281",
"fltt": "2",
"invt": "2",
"dect": "1",
"wbp2u": "|0|0|0|web",
"fid": "",
"fs": "b:MK0010",
"fields": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,"
"f23,f24,f25,f26,f22,f11,f62,f128,f136,f115,f152",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["diff"])
temp_df.reset_index(inplace=True)
temp_df["index"] = temp_df["index"].astype(int) + 1
temp_df.rename(
columns={
"index": "序号",
"f2": "最新价",
"f3": "涨跌幅",
"f4": "涨跌额",
"f5": "成交量",
"f6": "成交额",
"f7": "振幅",
"f10": "量比",
"f12": "代码",
"f14": "名称",
"f15": "最高",
"f16": "最低",
"f17": "今开",
"f18": "昨收",
},
inplace=True,
)
temp_df = temp_df[
[
"序号",
"代码",
"名称",
"最新价",
"涨跌幅",
"涨跌额",
"成交量",
"成交额",
"振幅",
"最高",
"最低",
"今开",
"昨收",
"量比",
]
]
temp_df["最新价"] = pd.to_numeric(temp_df["最新价"], errors="coerce")
temp_df["涨跌幅"] = pd.to_numeric(temp_df["涨跌幅"], errors="coerce")
temp_df["涨跌额"] = pd.to_numeric(temp_df["涨跌额"], errors="coerce")
temp_df["成交量"] = pd.to_numeric(temp_df["成交量"], errors="coerce")
temp_df["成交额"] = pd.to_numeric(temp_df["成交额"], errors="coerce")
temp_df["振幅"] = pd.to_numeric(temp_df["振幅"], errors="coerce")
temp_df["最高"] = pd.to_numeric(temp_df["最高"], errors="coerce")
temp_df["最低"] = pd.to_numeric(temp_df["最低"], errors="coerce")
temp_df["今开"] = pd.to_numeric(temp_df["今开"], errors="coerce")
temp_df["昨收"] = pd.to_numeric(temp_df["昨收"], errors="coerce")
temp_df["量比"] = pd.to_numeric(temp_df["量比"], errors="coerce")
return temp_df
def stock_zh_index_spot_em(symbol: str = "上证系列指数") -> pd.DataFrame:
"""
东方财富网-行情中心-沪深京指数
https://quote.eastmoney.com/center/gridlist.html#index_sz
:param symbol: "上证系列指数"; choice of {"沪深重要指数", "上证系列指数", "深证系列指数", "指数成份", "中证系列指数"}
:type symbol: str
:return: 指数的实时行情数据
:rtype: pandas.DataFrame
"""
if symbol == "沪深重要指数":
return __stock_zh_main_spot_em()
url = "https://48.push2.eastmoney.com/api/qt/clist/get"
symbol_map = {
"上证系列指数": "m:1+t:1",
"深证系列指数": "m:0 t:5",
"指数成份": "m:1+s:3,m:0+t:5",
"中证系列指数": "m:2",
}
params = {
"pn": "1",
"pz": "100",
"po": "1",
"np": "1",
"ut": "bd1d9ddb04089700cf9c27f6f7426281",
"fltt": "2",
"invt": "2",
"wbp2u": "|0|0|0|web",
"fid": "f12",
"fs": symbol_map[symbol],
"fields": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,"
"f26,f22,f33,f11,f62,f128,f136,f115,f152",
}
temp_df = fetch_paginated_data(url, params)
temp_df.rename(
columns={
"index": "序号",
"f2": "最新价",
"f3": "涨跌幅",
"f4": "涨跌额",
"f5": "成交量",
"f6": "成交额",
"f7": "振幅",
"f10": "量比",
"f12": "代码",
"f14": "名称",
"f15": "最高",
"f16": "最低",
"f17": "今开",
"f18": "昨收",
},
inplace=True,
)
temp_df = temp_df[
[
"序号",
"代码",
"名称",
"最新价",
"涨跌幅",
"涨跌额",
"成交量",
"成交额",
"振幅",
"最高",
"最低",
"今开",
"昨收",
"量比",
]
]
temp_df["最新价"] = pd.to_numeric(temp_df["最新价"], errors="coerce")
temp_df["涨跌幅"] = pd.to_numeric(temp_df["涨跌幅"], errors="coerce")
temp_df["涨跌额"] = pd.to_numeric(temp_df["涨跌额"], errors="coerce")
temp_df["成交量"] = pd.to_numeric(temp_df["成交量"], errors="coerce")
temp_df["成交额"] = pd.to_numeric(temp_df["成交额"], errors="coerce")
temp_df["振幅"] = pd.to_numeric(temp_df["振幅"], errors="coerce")
temp_df["最高"] = pd.to_numeric(temp_df["最高"], errors="coerce")
temp_df["最低"] = pd.to_numeric(temp_df["最低"], errors="coerce")
temp_df["今开"] = pd.to_numeric(temp_df["今开"], errors="coerce")
temp_df["昨收"] = pd.to_numeric(temp_df["昨收"], errors="coerce")
temp_df["量比"] = pd.to_numeric(temp_df["量比"], errors="coerce")
return temp_df
def stock_zh_index_daily(symbol: str = "sh000922") -> pd.DataFrame:
"""
新浪财经-指数-历史行情数据, 大量抓取容易封 IP
https://finance.sina.com.cn/realstock/company/sh000909/nc.shtml
:param symbol: sz399998, 指定指数代码
:type symbol: str
:return: 历史行情数据
:rtype: pandas.DataFrame
"""
params = {"d": "2020_2_4"}
res = requests.get(zh_sina_index_stock_hist_url.format(symbol), params=params)
js_code = py_mini_racer.MiniRacer()
js_code.eval(hk_js_decode)
dict_list = js_code.call(
"d", res.text.split("=")[1].split(";")[0].replace('"', "")
) # 执行js解密代码
temp_df = pd.DataFrame(dict_list)
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["volume"] = pd.to_numeric(temp_df["volume"], errors="coerce")
return temp_df
def get_tx_start_year(symbol: str = "sh000919") -> str:
"""
腾讯证券-获取所有股票数据的第一天, 注意这个数据是腾讯证券的历史数据第一天
https://gu.qq.com/sh000919/zs
:param symbol: 带市场标识的股票代码
:type symbol: str
:return: 开始日期
:rtype: str
"""
url = "https://web.ifzq.gtimg.cn/other/klineweb/klineWeb/weekTrends"
params = {
"code": symbol,
"type": "qfq",
"_var": "trend_qfq",
"r": "0.3506048543943414",
}
r = requests.get(url, params=params)
data_text = r.text
if not demjson.decode(data_text[data_text.find("={") + 1:])["data"]:
url = "https://proxy.finance.qq.com/ifzqgtimg/appstock/app/newfqkline/get"
params = {
"_var": "kline_dayqfq",
"param": f"{symbol},day,,,320,qfq",
"r": "0.751892490072597",
}
r = requests.get(url, params=params)
data_text = r.text
start_date = demjson.decode(data_text[data_text.find("={") + 1:])["data"][
symbol
]["day"][0][0]
return start_date
start_date = demjson.decode(data_text[data_text.find("={") + 1:])["data"][0][0]
return start_date
def stock_zh_index_daily_tx(
symbol: str = "sz980017",
start_date: str = "",
end_date: str = "",
) -> pd.DataFrame:
"""
腾讯证券-日频-股票或者指数历史数据(支持自定义时间范围)
作为 ak.stock_zh_index_daily() 的补充, 因为在新浪中有部分指数数据缺失
注意都是: 前复权, 不同网站复权方式不同, 不可混用数据
https://gu.qq.com/sh000919/zs
:param symbol: 带市场标识的股票或者指数代码
:type symbol: str
:param start_date: 开始日期, 格式 "YYYYMMDD", 为空则从最早日期开始
:type start_date: str
:param end_date: 结束日期, 格式 "YYYYMMDD", 为空则到当前日期
:type end_date: str
:return: 前复权的股票和指数数据
:rtype: pandas.DataFrame
"""
if start_date:
dt_start = datetime.datetime.strptime(start_date, "%Y%m%d")
i_start_year = dt_start.year
else:
earliest_date = get_tx_start_year(symbol=symbol)
dt_start = datetime.datetime.strptime(earliest_date, "%Y-%m-%d")
i_start_year = dt_start.year
if end_date:
dt_end = datetime.datetime.strptime(end_date, "%Y%m%d")
i_end_year = dt_end.year
else:
dt_end = datetime.datetime.combine(
datetime.date.today(), datetime.datetime.min.time()
)
i_end_year = dt_end.year
url = "https://proxy.finance.qq.com/ifzqgtimg/appstock/app/newfqkline/get"
temp_df = pd.DataFrame()
tqdm = get_tqdm()
for year in tqdm(range(i_start_year, i_end_year + 1), leave=False):
params = {
"_var": "kline_dayqfq",
"param": f"{symbol},day,{year}-01-01,{year + 1}-12-31,640,qfq",
"r": "0.8205512681390605",
}
res = requests.get(url, params=params)
text = res.text
try:
inner_temp_df = pd.DataFrame(
demjson.decode(text[text.find("={") + 1:])["data"][symbol]["day"]
)
except: # noqa: E722
inner_temp_df = pd.DataFrame(
demjson.decode(text[text.find("={") + 1:])["data"][symbol]["qfqday"]
)
temp_df = pd.concat(objs=[temp_df, inner_temp_df], ignore_index=True)
if temp_df.shape[1] == 6:
temp_df.columns = ["date", "open", "close", "high", "low", "amount"]
else:
temp_df = temp_df.iloc[:, :6]
temp_df.columns = ["date", "open", "close", "high", "low", "amount"]
temp_df["date"] = pd.to_datetime(temp_df["date"], errors="coerce").dt.date
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["amount"] = pd.to_numeric(temp_df["amount"], errors="coerce")
temp_df.drop_duplicates(inplace=True, ignore_index=True)
temp_df = temp_df[temp_df["date"] >= dt_start.date()]
temp_df = temp_df[temp_df["date"] <= dt_end.date()]
temp_df.reset_index(drop=True, inplace=True)
return temp_df
def stock_zh_index_daily_em(
symbol: str = "csi931151",
start_date: str = "19900101",
end_date: str = "20500101",
) -> pd.DataFrame:
"""
东方财富网-股票指数数据
https://quote.eastmoney.com/center/hszs.html
:param symbol: 带市场标识的指数代码; sz: 深交所, sh: 上交所, csi: 中信指数 + id(000905)
:type symbol: str
:param start_date: 开始时间
:type start_date: str
:param end_date: 结束时间
:type end_date: str
:return: 指数数据
:rtype: pandas.DataFrame
"""
market_map = {"sz": "0", "sh": "1", "csi": "2", "bj": "0"}
url = "https://push2his.eastmoney.com/api/qt/stock/kline/get"
if symbol.find("sz") != -1:
secid = "{}.{}".format(market_map["sz"], symbol.replace("sz", ""))
elif symbol.find("bj") != -1:
secid = "{}.{}".format(market_map["bj"], symbol.replace("bj", ""))
elif symbol.find("sh") != -1:
secid = "{}.{}".format(market_map["sh"], symbol.replace("sh", ""))
elif symbol.find("csi") != -1:
secid = "{}.{}".format(market_map["csi"], symbol.replace("csi", ""))
else:
return pd.DataFrame()
params = {
"secid": secid,
"fields1": "f1,f2,f3,f4,f5",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58",
"klt": "101", # 日频率
"fqt": "0",
"beg": start_date,
"end": end_date,
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame([item.split(",") for item in data_json["data"]["klines"]])
if temp_df.empty:
return pd.DataFrame()
temp_df.columns = ["date", "open", "close", "high", "low", "volume", "amount", "_"]
temp_df = temp_df[["date", "open", "close", "high", "low", "volume", "amount"]]
temp_df["open"] = pd.to_numeric(temp_df["open"], errors="coerce")
temp_df["close"] = pd.to_numeric(temp_df["close"], errors="coerce")
temp_df["high"] = pd.to_numeric(temp_df["high"], errors="coerce")
temp_df["low"] = pd.to_numeric(temp_df["low"], errors="coerce")
temp_df["volume"] = pd.to_numeric(temp_df["volume"], errors="coerce")
temp_df["amount"] = pd.to_numeric(temp_df["amount"], errors="coerce")
return temp_df
if __name__ == "__main__":
stock_zh_index_daily_df = stock_zh_index_daily(symbol="sh000510")
print(stock_zh_index_daily_df)
stock_zh_index_spot_sina_df = stock_zh_index_spot_sina()
print(stock_zh_index_spot_sina_df)
stock_zh_index_spot_em_df = stock_zh_index_spot_em(symbol="沪深重要指数")
print(stock_zh_index_spot_em_df)
stock_zh_index_spot_em_df = stock_zh_index_spot_em(symbol="上证系列指数")
print(stock_zh_index_spot_em_df)
stock_zh_index_spot_em_df = stock_zh_index_spot_em(symbol="深证系列指数")
print(stock_zh_index_spot_em_df)
stock_zh_index_spot_em_df = stock_zh_index_spot_em(symbol="指数成份")
print(stock_zh_index_spot_em_df)
stock_zh_index_spot_em_df = stock_zh_index_spot_em(symbol="中证系列指数")
print(stock_zh_index_spot_em_df)
stock_zh_index_daily_tx_df = stock_zh_index_daily_tx(symbol="sh000919", start_date="20260101", end_date="20260429")
print(stock_zh_index_daily_tx_df)
stock_zh_index_daily_em_df = stock_zh_index_daily_em(symbol="bj899050")
print(stock_zh_index_daily_em_df)
@@ -0,0 +1,115 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
"""
Date: 2024/12/23 17:00
Desc: 中证指数-所有指数-历史行情数据
https://www.csindex.com.cn/zh-CN/indices/index-detail/H30374#/indices/family/list?index_series=1
"""
import pandas as pd
import requests
def stock_zh_index_hist_csindex(
symbol: str = "000928",
start_date: str = "20180526",
end_date: str = "20240604",
) -> pd.DataFrame:
"""
中证指数-具体指数-历史行情数据
P.S. 只有收盘价正常情况下不应使用该接口除非指数只有中证网站有
https://www.csindex.com.cn/zh-CN/indices/index-detail/H30374#/indices/family/detail?indexCode=H30374
:param symbol: 指数代码; e.g., H30374
:type symbol: str
:param start_date: 开始日期
:type start_date: str
:param end_date: 结束日期
:type end_date: str
:return: 包含日期和收盘价的指数数据
:rtype: pandas.DataFrame
"""
url = "https://www.csindex.com.cn/csindex-home/perf/index-perf"
params = {
"indexCode": symbol,
"startDate": start_date,
"endDate": end_date,
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = [
"日期",
"指数代码",
"指数中文全称",
"指数中文简称",
"指数英文全称",
"指数英文简称",
"开盘",
"最高",
"最低",
"收盘",
"涨跌",
"涨跌幅",
"成交量",
"成交金额",
"样本数量",
"滚动市盈率",
]
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["开盘"] = pd.to_numeric(temp_df["开盘"], errors="coerce")
temp_df["最高"] = pd.to_numeric(temp_df["最高"], errors="coerce")
temp_df["最低"] = pd.to_numeric(temp_df["最低"], errors="coerce")
temp_df["收盘"] = pd.to_numeric(temp_df["收盘"], errors="coerce")
temp_df["涨跌"] = pd.to_numeric(temp_df["涨跌"], errors="coerce")
temp_df["涨跌幅"] = pd.to_numeric(temp_df["涨跌幅"], errors="coerce")
temp_df["成交量"] = pd.to_numeric(temp_df["成交量"], errors="coerce")
temp_df["成交金额"] = pd.to_numeric(temp_df["成交金额"], errors="coerce")
temp_df["样本数量"] = pd.to_numeric(temp_df["样本数量"], errors="coerce")
temp_df["滚动市盈率"] = pd.to_numeric(temp_df["滚动市盈率"], errors="coerce")
return temp_df
def stock_zh_index_value_csindex(symbol: str = "H30374") -> pd.DataFrame:
"""
中证指数-指数估值数据
https://www.csindex.com.cn/zh-CN/indices/index-detail/H30374#/indices/family/detail?indexCode=H30374
:param symbol: 指数代码; e.g., H30374
:type symbol: str
:return: 指数估值数据
:rtype: pandas.DataFrame
"""
url = (
f"https://oss-ch.csindex.com.cn/static/"
f"html/csindex/public/uploads/file/autofile/indicator/{symbol}indicator.xls"
)
temp_df = pd.read_excel(url)
temp_df.columns = [
"日期",
"指数代码",
"指数中文全称",
"指数中文简称",
"指数英文全称",
"指数英文简称",
"市盈率1",
"市盈率2",
"股息率1",
"股息率2",
]
temp_df["日期"] = pd.to_datetime(
temp_df["日期"], format="%Y%m%d", errors="coerce"
).dt.date
temp_df["市盈率1"] = pd.to_numeric(temp_df["市盈率1"], errors="coerce")
temp_df["市盈率2"] = pd.to_numeric(temp_df["市盈率2"], errors="coerce")
temp_df["股息率1"] = pd.to_numeric(temp_df["股息率1"], errors="coerce")
temp_df["股息率2"] = pd.to_numeric(temp_df["股息率2"], errors="coerce")
return temp_df
if __name__ == "__main__":
stock_zh_index_hist_csindex_df = stock_zh_index_hist_csindex(
symbol="000928", start_date="20100101", end_date="20240604"
)
print(stock_zh_index_hist_csindex_df)
stock_zh_index_value_csindex_df = stock_zh_index_value_csindex(symbol="H30374")
print(stock_zh_index_value_csindex_df)
@@ -0,0 +1,129 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
"""
Date: 2023/6/19 17:00
Desc: 沐甜科技数据中心-中国食糖指数
https://www.msweet.com.cn/mtkj/sjzx13/index.html
"""
import requests
import pandas as pd
def index_sugar_msweet() -> pd.DataFrame:
"""
沐甜科技数据中心-中国食糖指数
https://www.msweet.com.cn/mtkj/sjzx13/index.html
:return: 中国食糖指数
:rtype: pandas.DataFrame
"""
url = "https://www.msweet.com.cn/eportal/ui"
params = {
"struts.portlet.action": "/portlet/price!getSTZSJson.action",
"moduleId": "cb752447cfe24b44b18c7a7e9abab048",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.concat(
[pd.DataFrame(data_json["category"]), pd.DataFrame(data_json["data"])], axis=1
)
temp_df.columns = ["日期", "综合价格", "原糖价格", "现货价格"]
temp_df.loc[3226, ["原糖价格"]] = 12.88 # 数据源错误
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["综合价格"] = pd.to_numeric(temp_df["综合价格"], errors="coerce")
temp_df["原糖价格"] = pd.to_numeric(temp_df["原糖价格"], errors="coerce")
temp_df["现货价格"] = pd.to_numeric(temp_df["现货价格"], errors="coerce")
return temp_df
def index_inner_quote_sugar_msweet() -> pd.DataFrame:
"""
沐甜科技数据中心-配额内进口糖估算指数
https://www.msweet.com.cn/mtkj/sjzx13/index.html
:return: 配额内进口糖估算指数
:rtype: pandas.DataFrame
"""
url = "https://www.msweet.com.cn/datacenterapply/datacenter/json/JinKongTang.json"
r = requests.get(url)
data_json = r.json()
temp_df = pd.concat(
[pd.DataFrame(data_json["category"]), pd.DataFrame(data_json["data"])], axis=1
)
temp_df.columns = [
"日期",
"利润空间",
"泰国糖",
"泰国MA5",
"巴西MA5",
"利润MA5",
"巴西MA10",
"巴西糖",
"柳州现货价",
"广州现货价",
"泰国MA10",
"利润MA30",
"利润MA10",
]
temp_df.loc[988, ["泰国糖"]] = 4045.2 # 数据源错误
temp_df["日期"] = temp_df["日期"].str.replace("/", "-")
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["利润空间"] = pd.to_numeric(temp_df["利润空间"], errors="coerce")
temp_df["泰国糖"] = pd.to_numeric(temp_df["泰国糖"], errors="coerce")
temp_df["泰国MA5"] = pd.to_numeric(temp_df["泰国MA5"], errors="coerce")
temp_df["巴西MA5"] = pd.to_numeric(temp_df["巴西MA5"], errors="coerce")
temp_df["巴西MA10"] = pd.to_numeric(temp_df["巴西MA10"], errors="coerce")
temp_df["巴西糖"] = pd.to_numeric(temp_df["巴西糖"], errors="coerce")
temp_df["柳州现货价"] = pd.to_numeric(temp_df["柳州现货价"], errors="coerce")
temp_df["广州现货价"] = pd.to_numeric(temp_df["广州现货价"], errors="coerce")
temp_df["泰国MA10"] = pd.to_numeric(temp_df["泰国MA10"], errors="coerce")
temp_df["利润MA30"] = pd.to_numeric(temp_df["利润MA30"], errors="coerce")
temp_df["利润MA10"] = pd.to_numeric(temp_df["利润MA10"], errors="coerce")
return temp_df
def index_outer_quote_sugar_msweet() -> pd.DataFrame:
"""
沐甜科技数据中心-配额外进口糖估算指数
https://www.msweet.com.cn/mtkj/sjzx13/index.html
:return: 配额内进口糖估算指数
:rtype: pandas.DataFrame
"""
url = "https://www.msweet.com.cn/datacenterapply/datacenter/json/Jkpewlr.json"
r = requests.get(url)
data_json = r.json()
temp_df = pd.concat(
[pd.DataFrame(data_json["category"]), pd.DataFrame(data_json["data"])], axis=1
)
temp_df.columns = [
"日期",
"巴西糖进口成本",
"泰国糖进口利润空间",
"巴西糖进口利润空间",
"泰国糖进口成本",
"日照现货价",
]
temp_df["日期"] = temp_df["日期"].str.replace("/", "-")
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["巴西糖进口成本"] = pd.to_numeric(
temp_df["巴西糖进口成本"], errors="coerce"
)
temp_df["泰国糖进口利润空间"] = pd.to_numeric(
temp_df["泰国糖进口利润空间"], errors="coerce"
)
temp_df["巴西糖进口利润空间"] = pd.to_numeric(
temp_df["巴西糖进口利润空间"], errors="coerce"
)
temp_df["泰国糖进口成本"] = pd.to_numeric(temp_df["泰国糖进口成本"])
temp_df["日照现货价"] = pd.to_numeric(temp_df["日照现货价"], errors="coerce")
return temp_df
if __name__ == "__main__":
index_sugar_msweet_df = index_sugar_msweet()
print(index_sugar_msweet_df)
index_inner_quote_sugar_msweet_df = index_inner_quote_sugar_msweet()
print(index_inner_quote_sugar_msweet_df)
index_outer_quote_sugar_msweet_df = index_outer_quote_sugar_msweet()
print(index_outer_quote_sugar_msweet_df)
@@ -0,0 +1,278 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/1/24 15:00
Desc: 申万宏源研究-申万指数-指数发布
乐咕乐股网
https://legulegu.com/stockdata/index-composition?industryCode=851921.SI
"""
from io import StringIO
import pandas as pd
import requests
from bs4 import BeautifulSoup
from akshare.utils.cons import headers
def sw_index_first_info() -> pd.DataFrame:
"""
乐咕乐股-申万一级-分类
https://legulegu.com/stockdata/sw-industry-overview#level1
:return: 分类
:rtype: pandas.DataFrame
"""
url = "https://legulegu.com/stockdata/sw-industry-overview"
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, features="lxml")
code_raw = soup.find(name="div", attrs={"id": "level1Items"}).find_all(
name="div", attrs={"class": "lg-industries-item-chinese-title"}
)
name_raw = soup.find(name="div", attrs={"id": "level1Items"}).find_all(
name="div", attrs={"class": "lg-industries-item-number"}
)
value_raw = soup.find(name="div", attrs={"id": "level1Items"}).find_all(
name="div", attrs={"class": "lg-sw-industries-item-value"}
)
code = [item.get_text() for item in code_raw]
name = [item.get_text().split("(")[0] for item in name_raw]
num = [item.get_text().split("(")[1].split(")")[0] for item in name_raw]
num_1 = [
item.find_all("span", attrs={"class": "value"})[0].get_text().strip()
for item in value_raw
]
num_2 = [
item.find_all("span", attrs={"class": "value"})[1].get_text().strip()
for item in value_raw
]
num_3 = [
item.find_all("span", attrs={"class": "value"})[2].get_text().strip()
for item in value_raw
]
num_4 = [
item.find_all("span", attrs={"class": "value"})[3].get_text().strip()
for item in value_raw
]
temp_df = pd.DataFrame([code, name, num, num_1, num_2, num_3, num_4]).T
temp_df.columns = [
"行业代码",
"行业名称",
"成份个数",
"静态市盈率",
"TTM(滚动)市盈率",
"市净率",
"静态股息率",
]
temp_df["成份个数"] = pd.to_numeric(temp_df["成份个数"], errors="coerce")
temp_df["静态市盈率"] = pd.to_numeric(temp_df["静态市盈率"], errors="coerce")
temp_df["TTM(滚动)市盈率"] = pd.to_numeric(
temp_df["TTM(滚动)市盈率"], errors="coerce"
)
temp_df["市净率"] = pd.to_numeric(temp_df["市净率"], errors="coerce")
temp_df["静态股息率"] = pd.to_numeric(temp_df["静态股息率"], errors="coerce")
return temp_df
def sw_index_second_info() -> pd.DataFrame:
"""
乐咕乐股-申万二级-分类
https://legulegu.com/stockdata/sw-industry-overview#level1
:return: 分类
:rtype: pandas.DataFrame
"""
url = "https://legulegu.com/stockdata/sw-industry-overview"
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, features="lxml")
code_raw = soup.find(name="div", attrs={"id": "level2Items"}).find_all(
name="div", attrs={"class": "lg-industries-item-chinese-title"}
)
name_raw = soup.find(name="div", attrs={"id": "level2Items"}).find_all(
name="div", attrs={"class": "lg-industries-item-number"}
)
value_raw = soup.find(name="div", attrs={"id": "level2Items"}).find_all(
name="div", attrs={"class": "lg-sw-industries-item-value"}
)
code = [item.get_text() for item in code_raw]
name = [item.get_text().split("(")[0] for item in name_raw]
parent_name = [
item.find("span").get_text().split("(")[0][1:-1] for item in name_raw
]
num = [item.get_text().split("(")[1].split(")")[0] for item in name_raw]
num_1 = [
item.find_all("span", attrs={"class": "value"})[0].get_text().strip()
for item in value_raw
]
num_2 = [
item.find_all("span", attrs={"class": "value"})[1].get_text().strip()
for item in value_raw
]
num_3 = [
item.find_all("span", attrs={"class": "value"})[2].get_text().strip()
for item in value_raw
]
num_4 = [
item.find_all("span", attrs={"class": "value"})[3].get_text().strip()
for item in value_raw
]
temp_df = pd.DataFrame([code, name, parent_name, num, num_1, num_2, num_3, num_4]).T
temp_df.columns = [
"行业代码",
"行业名称",
"上级行业",
"成份个数",
"静态市盈率",
"TTM(滚动)市盈率",
"市净率",
"静态股息率",
]
temp_df["成份个数"] = pd.to_numeric(temp_df["成份个数"], errors="coerce")
temp_df["静态市盈率"] = pd.to_numeric(temp_df["静态市盈率"], errors="coerce")
temp_df["TTM(滚动)市盈率"] = pd.to_numeric(
temp_df["TTM(滚动)市盈率"], errors="coerce"
)
temp_df["市净率"] = pd.to_numeric(temp_df["市净率"], errors="coerce")
temp_df["静态股息率"] = pd.to_numeric(temp_df["静态股息率"], errors="coerce")
return temp_df
def sw_index_third_info() -> pd.DataFrame:
"""
乐咕乐股-申万三级-分类
https://legulegu.com/stockdata/sw-industry-overview#level1
:return: 分类
:rtype: pandas.DataFrame
"""
url = "https://legulegu.com/stockdata/sw-industry-overview"
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, features="lxml")
code_raw = soup.find(name="div", attrs={"id": "level3Items"}).find_all(
name="div", attrs={"class": "lg-industries-item-chinese-title"}
)
name_raw = soup.find(name="div", attrs={"id": "level3Items"}).find_all(
name="div", attrs={"class": "lg-industries-item-number"}
)
value_raw = soup.find(name="div", attrs={"id": "level3Items"}).find_all(
name="div", attrs={"class": "lg-sw-industries-item-value"}
)
code = [item.get_text() for item in code_raw]
name = [item.get_text().split("(")[0] for item in name_raw]
parent_name = [
item.find("span").get_text().split("(")[0][1:-1] for item in name_raw
]
num = [item.get_text().split("(")[1].split(")")[0] for item in name_raw]
num_1 = [
item.find_all("span", attrs={"class": "value"})[0].get_text().strip()
for item in value_raw
]
num_2 = [
item.find_all("span", attrs={"class": "value"})[1].get_text().strip()
for item in value_raw
]
num_3 = [
item.find_all("span", attrs={"class": "value"})[2].get_text().strip()
for item in value_raw
]
num_4 = [
item.find_all("span", attrs={"class": "value"})[3].get_text().strip()
for item in value_raw
]
temp_df = pd.DataFrame([code, name, parent_name, num, num_1, num_2, num_3, num_4]).T
temp_df.columns = [
"行业代码",
"行业名称",
"上级行业",
"成份个数",
"静态市盈率",
"TTM(滚动)市盈率",
"市净率",
"静态股息率",
]
temp_df["成份个数"] = pd.to_numeric(temp_df["成份个数"], errors="coerce")
temp_df["静态市盈率"] = pd.to_numeric(temp_df["静态市盈率"], errors="coerce")
temp_df["TTM(滚动)市盈率"] = pd.to_numeric(
temp_df["TTM(滚动)市盈率"], errors="coerce"
)
temp_df["市净率"] = pd.to_numeric(temp_df["市净率"], errors="coerce")
temp_df["静态股息率"] = pd.to_numeric(temp_df["静态股息率"], errors="coerce")
return temp_df
def sw_index_third_cons(symbol: str = "801120.SI") -> pd.DataFrame:
"""
乐咕乐股-申万三级-行业成份
https://legulegu.com/stockdata/index-composition?industryCode=801120.SI
:param symbol: 三级行业的行业代码
:type symbol: str
:return: 行业成份
:rtype: pandas.DataFrame
"""
url = f"https://legulegu.com/stockdata/index-composition?industryCode={symbol}"
r = requests.get(url, headers=headers)
temp_df = pd.read_html(StringIO(r.text))[0]
temp_df.columns = [
"序号",
"股票代码",
"股票简称",
"纳入时间",
"申万1级",
"申万2级",
"申万3级",
"价格",
"市盈率",
"市盈率ttm",
"市净率",
"股息率",
"市值",
"归母净利润同比增长(09-30)",
"归母净利润同比增长(06-30)",
"营业收入同比增长(09-30)",
"营业收入同比增长(06-30)",
]
temp_df["价格"] = pd.to_numeric(temp_df["价格"], errors="coerce")
temp_df["市盈率"] = pd.to_numeric(temp_df["市盈率"], errors="coerce")
temp_df["市盈率ttm"] = pd.to_numeric(temp_df["市盈率ttm"], errors="coerce")
temp_df["市净率"] = pd.to_numeric(temp_df["市净率"], errors="coerce")
temp_df["股息率"] = pd.to_numeric(temp_df["股息率"].str.strip("%"), errors="coerce")
temp_df["市值"] = pd.to_numeric(temp_df["市值"], errors="coerce")
temp_df["归母净利润同比增长(09-30)"] = temp_df[
"归母净利润同比增长(09-30)"
].str.strip("%")
temp_df["归母净利润同比增长(06-30)"] = temp_df[
"归母净利润同比增长(06-30)"
].str.strip("%")
temp_df["营业收入同比增长(09-30)"] = temp_df["营业收入同比增长(09-30)"].str.strip(
"%"
)
temp_df["营业收入同比增长(06-30)"] = temp_df["营业收入同比增长(06-30)"].str.strip(
"%"
)
temp_df["归母净利润同比增长(09-30)"] = pd.to_numeric(
temp_df["归母净利润同比增长(09-30)"], errors="coerce"
)
temp_df["归母净利润同比增长(06-30)"] = pd.to_numeric(
temp_df["归母净利润同比增长(06-30)"], errors="coerce"
)
temp_df["营业收入同比增长(09-30)"] = pd.to_numeric(
temp_df["营业收入同比增长(09-30)"], errors="coerce"
)
temp_df["营业收入同比增长(06-30)"] = pd.to_numeric(
temp_df["营业收入同比增长(06-30)"], errors="coerce"
)
return temp_df
if __name__ == "__main__":
sw_index_first_info_df = sw_index_first_info()
print(sw_index_first_info_df)
sw_index_second_info_df = sw_index_second_info()
print(sw_index_second_info_df)
sw_index_third_info_df = sw_index_third_info()
print(sw_index_third_info_df)
sw_index_third_cons_df = sw_index_third_cons(symbol="850111.SI")
print(sw_index_third_cons_df)
@@ -0,0 +1,96 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2025/5/3
Desc: 义乌小商品指数
目前可以通过这些接口直接请求到 JSON 数据
周价格指数https://apiserver.chinagoods.com/yiwuindex/v1/active/industry/class/history/piweek?gcCode=
月价格指数https://apiserver.chinagoods.com/yiwuindex/v1/active/industry/class/history/month?gcCode=
月景气指数https://apiserver.chinagoods.com/yiwuindex/v1/active/industry/class/history/bi?gcCode=
上涨https://apiserver.chinagoods.com/yiwuindex/v1/active/industry/class/get/rise
下跌https://apiserver.chinagoods.com/yiwuindex/v1/active/industry/class/get/drop
"""
import pandas as pd
import requests
def index_yw(symbol: str = "月景气指数") -> pd.DataFrame:
"""
义乌小商品指数
https://www.ywindex.com/Home/Product/index/
:param symbol: choice of {"周价格指数", "月价格指数", "月景气指数"}
:type symbol: str
:return: 指数结果
:rtype: pandas.DataFrame
"""
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
if symbol == "月景气指数":
url = "https://apiserver.chinagoods.com/yiwuindex/v1/active/industry/class/history/bi?gcCode="
r = requests.get(url, verify=False)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"])
temp_df = temp_df[
[
"indextimeno",
"totalindex",
"scopeindex",
"benifitindex",
"confidentindex",
]
]
temp_df.columns = ["期数", "景气指数", "规模指数", "效益指数", "市场信心指数"]
temp_df["期数"] = pd.to_datetime(temp_df["期数"], errors="coerce").dt.date
temp_df["景气指数"] = pd.to_numeric(temp_df["景气指数"], errors="coerce")
temp_df["规模指数"] = pd.to_numeric(temp_df["规模指数"], errors="coerce")
temp_df["效益指数"] = pd.to_numeric(temp_df["效益指数"], errors="coerce")
temp_df["市场信心指数"] = pd.to_numeric(
temp_df["市场信心指数"], errors="coerce"
)
temp_df.sort_values(["期数"], inplace=True, ignore_index=True)
return temp_df
else:
symbol_map = {"周价格指数": "piweek", "月价格指数": "month"}
url = f"https://apiserver.chinagoods.com/yiwuindex/v1/active/industry/class/history/{symbol_map[symbol]}?gcCode="
r = requests.get(url, verify=False)
data_json = r.json()
columns_name = {
"indextimeno": "期数",
"totalpriceindex": "价格指数",
"stockdealpriceindex": "场内价格指数",
"netdealpriceindex": "网上价格指数",
"orderdealpriceindex": "订单价格指数",
"outdealpriceindex": "出口价格指数",
}
temp_df = pd.DataFrame(data_json["data"])
temp_df.columns = [columns_name[name] for name in temp_df.columns]
temp_df["期数"] = pd.to_datetime(temp_df["期数"], errors="coerce").dt.date
temp_df["价格指数"] = pd.to_numeric(temp_df["价格指数"], errors="coerce")
temp_df["场内价格指数"] = pd.to_numeric(
temp_df["场内价格指数"], errors="coerce"
)
temp_df["网上价格指数"] = pd.to_numeric(
temp_df["网上价格指数"], errors="coerce"
)
temp_df["订单价格指数"] = pd.to_numeric(
temp_df["订单价格指数"], errors="coerce"
)
temp_df["出口价格指数"] = pd.to_numeric(
temp_df["出口价格指数"], errors="coerce"
)
temp_df.sort_values(by=["期数"], inplace=True, ignore_index=True)
return temp_df
if __name__ == "__main__":
index_yw_df = index_yw(symbol="周价格指数")
print(index_yw_df)
index_yw_df = index_yw(symbol="月价格指数")
print(index_yw_df)
index_yw_df = index_yw(symbol="月景气指数")
print(index_yw_df)
@@ -0,0 +1,48 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2024/4/17 19:00
Desc: 数库-A股新闻情绪指数
https://www.chinascope.com/reasearch.html
"""
import pandas as pd
import requests
def index_news_sentiment_scope() -> pd.DataFrame:
"""
数库-A股新闻情绪指数
https://www.chinascope.com/reasearch.html
:return: A股新闻情绪指数
:rtype: pandas.DataFrame
"""
url = "https://www.chinascope.com/inews/senti/index"
params = {"period": "YEAR"}
r = requests.get(url=url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json)
temp_df.rename(
columns={
"tradeDate": "日期",
"maIndex1": "市场情绪指数",
"marketClose": "沪深300指数",
},
inplace=True,
)
temp_df = temp_df[
[
"日期",
"市场情绪指数",
"沪深300指数",
]
]
temp_df["日期"] = pd.to_datetime(temp_df["日期"], errors="coerce").dt.date
temp_df["市场情绪指数"] = pd.to_numeric(temp_df["市场情绪指数"], errors="coerce")
temp_df["沪深300指数"] = pd.to_numeric(temp_df["沪深300指数"], errors="coerce")
return temp_df
if __name__ == "__main__":
index_news_sentiment_scope_df = index_news_sentiment_scope()
print(index_news_sentiment_scope_df)
@@ -0,0 +1,379 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2025/3/11 17:00
Desc: 东方财富网-指数行情数据
"""
from functools import lru_cache
import pandas as pd
import requests
from akshare.utils.func import fetch_paginated_data
@lru_cache()
def index_code_id_map_em() -> dict:
"""
东方财富-股票和市场代码
https://quote.eastmoney.com/center/gridlist.html#hs_a_board
:return: 股票和市场代码
:rtype: dict
"""
url = "https://80.push2.eastmoney.com/api/qt/clist/get"
params = {
"pn": "1",
"pz": "100",
"po": "1",
"np": "1",
"ut": "bd1d9ddb04089700cf9c27f6f7426281",
"fltt": "2",
"invt": "2",
"fid": "f3",
"fs": "b:MK0010,m:1+t:1,m:0 t:5,m:1+s:3,m:0+t:5,m:2",
"fields": "f3,f12,f13",
}
temp_df = fetch_paginated_data(url, params)
code_id_dict = dict(zip(temp_df["f12"], temp_df["f13"]))
return code_id_dict
def index_zh_a_hist(
symbol: str = "000859",
period: str = "daily",
start_date: str = "19700101",
end_date: str = "22220101",
) -> pd.DataFrame:
"""
东方财富网-中国股票指数-行情数据
https://quote.eastmoney.com/zz/2.000859.html
:param symbol: 指数代码
:type symbol: str
:param period: choice of {'daily', 'weekly', 'monthly'}
:type period: str
:param start_date: 开始日期
:type start_date: str
:param end_date: 结束日期
:type end_date: str
:return: 行情数据
:rtype: pandas.DataFrame
"""
code_id_dict = index_code_id_map_em()
period_dict = {"daily": "101", "weekly": "102", "monthly": "103"}
url = "https://push2his.eastmoney.com/api/qt/stock/kline/get"
try:
params = {
"secid": f"{code_id_dict[symbol]}.{symbol}",
"ut": "7eea3edcaed734bea9cbfc24409ed989",
"fields1": "f1,f2,f3,f4,f5,f6",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61",
"klt": period_dict[period],
"fqt": "0",
"beg": "0",
"end": "20500000",
}
except KeyError:
params = {
"secid": f"1.{symbol}",
"ut": "7eea3edcaed734bea9cbfc24409ed989",
"fields1": "f1,f2,f3,f4,f5,f6",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61",
"klt": period_dict[period],
"fqt": "0",
"beg": "0",
"end": "20500000",
}
r = requests.get(url, params=params)
data_json = r.json()
if data_json["data"] is None:
params = {
"secid": f"0.{symbol}",
"ut": "7eea3edcaed734bea9cbfc24409ed989",
"fields1": "f1,f2,f3,f4,f5,f6",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61",
"klt": period_dict[period],
"fqt": "0",
"beg": "0",
"end": "20500000",
}
r = requests.get(url, params=params)
data_json = r.json()
if data_json["data"] is None:
params = {
"secid": f"2.{symbol}",
"ut": "7eea3edcaed734bea9cbfc24409ed989",
"fields1": "f1,f2,f3,f4,f5,f6",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61",
"klt": period_dict[period],
"fqt": "0",
"beg": "0",
"end": "20500000",
}
r = requests.get(url, params=params)
data_json = r.json()
if data_json["data"] is None:
params = {
"secid": f"47.{symbol}",
"ut": "7eea3edcaed734bea9cbfc24409ed989",
"fields1": "f1,f2,f3,f4,f5,f6",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61",
"klt": period_dict[period],
"fqt": "0",
"beg": "0",
"end": "20500000",
}
r = requests.get(url, params=params)
data_json = r.json()
try:
temp_df = pd.DataFrame(
[item.split(",") for item in data_json["data"]["klines"]]
)
except: # noqa: E722
# 兼容 000859(中证国企一路一带) 和 000861(中证央企创新)
params = {
"secid": f"2.{symbol}",
"ut": "7eea3edcaed734bea9cbfc24409ed989",
"fields1": "f1,f2,f3,f4,f5,f6",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61",
"klt": period_dict[period],
"fqt": "0",
"beg": "0",
"end": "20500000",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(
[item.split(",") for item in data_json["data"]["klines"]]
)
temp_df.columns = [
"日期",
"开盘",
"收盘",
"最高",
"最低",
"成交量",
"成交额",
"振幅",
"涨跌幅",
"涨跌额",
"换手率",
]
temp_df.index = pd.to_datetime(temp_df["日期"], errors="coerce")
temp_df = temp_df[start_date:end_date]
temp_df.reset_index(inplace=True, drop=True)
temp_df["开盘"] = pd.to_numeric(temp_df["开盘"], errors="coerce")
temp_df["收盘"] = pd.to_numeric(temp_df["收盘"], errors="coerce")
temp_df["最高"] = pd.to_numeric(temp_df["最高"], errors="coerce")
temp_df["最低"] = pd.to_numeric(temp_df["最低"], errors="coerce")
temp_df["成交量"] = pd.to_numeric(temp_df["成交量"], errors="coerce")
temp_df["成交额"] = pd.to_numeric(temp_df["成交额"], errors="coerce")
temp_df["振幅"] = pd.to_numeric(temp_df["振幅"], errors="coerce")
temp_df["涨跌幅"] = pd.to_numeric(temp_df["涨跌幅"], errors="coerce")
temp_df["涨跌额"] = pd.to_numeric(temp_df["涨跌额"], errors="coerce")
temp_df["换手率"] = pd.to_numeric(temp_df["换手率"], errors="coerce")
return temp_df
def index_zh_a_hist_min_em(
symbol: str = "399006",
period: str = "1",
start_date: str = "1979-09-01 09:32:00",
end_date: str = "2222-01-01 09:32:00",
) -> pd.DataFrame:
"""
东方财富网-指数数据-每日分时行情
https://quote.eastmoney.com/center/hszs.html
:param symbol: 指数代码
:type symbol: str
:param period: choice of {'1', '5', '15', '30', '60'}
:type period: str
:param start_date: 开始日期
:type start_date: str
:param end_date: 结束日期
:type end_date: str
:return: 每日分时行情
:rtype: pandas.DataFrame
"""
code_id_dict = index_code_id_map_em()
if period == "1":
url = "https://push2his.eastmoney.com/api/qt/stock/trends2/get"
try:
params = {
"fields1": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58",
"iscr": "0",
"ndays": "5",
"secid": f"{code_id_dict[symbol]}.{symbol}",
}
except KeyError:
params = {
"fields1": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58",
"iscr": "0",
"ndays": "5",
"secid": f"1.{symbol}",
}
r = requests.get(url, params=params)
data_json = r.json()
if data_json["data"] is None:
params = {
"fields1": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58",
"iscr": "0",
"ndays": "5",
"secid": f"0.{symbol}",
}
r = requests.get(url, params=params)
data_json = r.json()
if data_json["data"] is None:
params = {
"fields1": "f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58",
"iscr": "0",
"ndays": "5",
"secid": f"47.{symbol}",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(
[item.split(",") for item in data_json["data"]["trends"]]
)
temp_df.columns = [
"时间",
"开盘",
"收盘",
"最高",
"最低",
"成交量",
"成交额",
"均价",
]
temp_df.index = pd.to_datetime(temp_df["时间"], errors="coerce")
temp_df = temp_df[start_date:end_date]
temp_df.reset_index(drop=True, inplace=True)
temp_df["开盘"] = pd.to_numeric(temp_df["开盘"], errors="coerce")
temp_df["收盘"] = pd.to_numeric(temp_df["收盘"], errors="coerce")
temp_df["最高"] = pd.to_numeric(temp_df["最高"], errors="coerce")
temp_df["最低"] = pd.to_numeric(temp_df["最低"], errors="coerce")
temp_df["成交量"] = pd.to_numeric(temp_df["成交量"], errors="coerce")
temp_df["成交额"] = pd.to_numeric(temp_df["成交额"], errors="coerce")
temp_df["均价"] = pd.to_numeric(temp_df["均价"], errors="coerce")
temp_df["时间"] = pd.to_datetime(temp_df["时间"]).astype(str)
return temp_df
else:
url = "https://push2his.eastmoney.com/api/qt/stock/kline/get"
try:
params = {
"secid": f"{code_id_dict[symbol]}.{symbol}",
"ut": "7eea3edcaed734bea9cbfc24409ed989",
"fields1": "f1,f2,f3,f4,f5,f6",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61",
"klt": period,
"fqt": "1",
"beg": "0",
"end": "20500000",
}
except: # noqa: E722
params = {
"secid": f"0.{symbol}",
"ut": "7eea3edcaed734bea9cbfc24409ed989",
"fields1": "f1,f2,f3,f4,f5,f6",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61",
"klt": period,
"fqt": "1",
"beg": "0",
"end": "20500000",
}
r = requests.get(url, params=params)
data_json = r.json()
if data_json["data"] is None:
params = {
"secid": f"1.{symbol}",
"ut": "7eea3edcaed734bea9cbfc24409ed989",
"fields1": "f1,f2,f3,f4,f5,f6",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61",
"klt": period,
"fqt": "1",
"beg": "0",
"end": "20500000",
}
r = requests.get(url, params=params)
data_json = r.json()
if data_json["data"] is None:
params = {
"secid": f"47.{symbol}",
"ut": "7eea3edcaed734bea9cbfc24409ed989",
"fields1": "f1,f2,f3,f4,f5,f6",
"fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61",
"klt": period,
"fqt": "1",
"beg": "0",
"end": "20500000",
}
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(
[item.split(",") for item in data_json["data"]["klines"]]
)
temp_df.columns = [
"时间",
"开盘",
"收盘",
"最高",
"最低",
"成交量",
"成交额",
"振幅",
"涨跌幅",
"涨跌额",
"换手率",
]
temp_df.index = pd.to_datetime(temp_df["时间"], errors="coerce")
temp_df = temp_df[start_date:end_date]
temp_df.reset_index(drop=True, inplace=True)
temp_df["开盘"] = pd.to_numeric(temp_df["开盘"], errors="coerce")
temp_df["收盘"] = pd.to_numeric(temp_df["收盘"], errors="coerce")
temp_df["最高"] = pd.to_numeric(temp_df["最高"], errors="coerce")
temp_df["最低"] = pd.to_numeric(temp_df["最低"], errors="coerce")
temp_df["成交量"] = pd.to_numeric(temp_df["成交量"], errors="coerce")
temp_df["成交额"] = pd.to_numeric(temp_df["成交额"], errors="coerce")
temp_df["振幅"] = pd.to_numeric(temp_df["振幅"], errors="coerce")
temp_df["涨跌幅"] = pd.to_numeric(temp_df["涨跌幅"], errors="coerce")
temp_df["涨跌额"] = pd.to_numeric(temp_df["涨跌额"], errors="coerce")
temp_df["换手率"] = pd.to_numeric(temp_df["换手率"], errors="coerce")
temp_df["时间"] = pd.to_datetime(temp_df["时间"], errors="coerce").astype(str)
temp_df = temp_df[
[
"时间",
"开盘",
"收盘",
"最高",
"最低",
"涨跌幅",
"涨跌额",
"成交量",
"成交额",
"振幅",
"换手率",
]
]
return temp_df
if __name__ == "__main__":
index_zh_a_hist_df = index_zh_a_hist(
symbol="932000",
period="daily",
start_date="19700101",
end_date="22220101",
)
print(index_zh_a_hist_df)
index_zh_a_hist_min_em_df = index_zh_a_hist_min_em(
symbol="000003",
period="1",
start_date="2025-03-17 09:30:00",
end_date="2025-03-17 19:00:00",
)
print(index_zh_a_hist_min_em_df)