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 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
"""
Date: 2021/12/6 21:58
Desc:
"""
@@ -0,0 +1,232 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
"""
Date: 2024/8/10 15:30
Desc: 搜猪-生猪大数据-各省均价实时排行榜
https://www.soozhu.com/price/data/center/
"""
import pandas as pd
import requests
from bs4 import BeautifulSoup
def spot_hog_soozhu() -> pd.DataFrame:
"""
搜猪-生猪大数据-各省均价实时排行榜
https://www.soozhu.com/price/data/center/
:return: 各省均价实时排行榜
:rtype: pandas.DataFrame
"""
session = requests.session()
url = "https://www.soozhu.com/price/data/center/"
r = session.get(url)
soup = BeautifulSoup(r.text, features="lxml")
token = soup.find(name="input", attrs={"name": "csrfmiddlewaretoken"})["value"]
url = "https://www.soozhu.com/price/data/center/"
payload = {"act": "mapdata", "csrfmiddlewaretoken": token}
r = session.post(url, data=payload)
data_json = r.json()
temp_df = pd.DataFrame(data_json["vlist"])
price_list = [item[0] for item in temp_df["value"]]
pct_list = [item[1] for item in temp_df["value"]]
big_df = pd.DataFrame([temp_df["name"].values, price_list, pct_list]).T
big_df.columns = ["省份", "价格", "涨跌幅"]
big_df["价格"] = pd.to_numeric(big_df["价格"], errors="coerce")
big_df["涨跌幅"] = pd.to_numeric(big_df["涨跌幅"], errors="coerce")
big_df["涨跌幅"] = round(big_df["涨跌幅"], 2)
return big_df
def spot_hog_year_trend_soozhu() -> pd.DataFrame:
"""
搜猪-生猪大数据-今年以来全国出栏均价走势
https://www.soozhu.com/price/data/center/
:return: 今年以来全国出栏均价走势
:rtype: pandas.DataFrame
"""
session = requests.session()
url = "https://www.soozhu.com/price/data/center/"
r = session.get(url)
soup = BeautifulSoup(r.text, features="lxml")
token = soup.find(name="input", attrs={"name": "csrfmiddlewaretoken"})["value"]
url = "https://www.soozhu.com/price/data/center/"
payload = {"act": "yeartrend", "csrfmiddlewaretoken": token}
r = session.post(url, data=payload)
data_json = r.json()
temp_df = pd.DataFrame(data_json["nationlist"])
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.sort_values(by=["日期"], ignore_index=True, inplace=True)
return temp_df
def spot_hog_lean_price_soozhu() -> pd.DataFrame:
"""
搜猪-生猪大数据-全国瘦肉型肉猪
https://www.soozhu.com/price/data/center/
:return: 全国瘦肉型肉猪
:rtype: pandas.DataFrame
"""
session = requests.session()
url = "https://www.soozhu.com/price/data/center/"
r = session.get(url)
soup = BeautifulSoup(r.text, features="lxml")
token = soup.find(name="input", attrs={"name": "csrfmiddlewaretoken"})["value"]
url = "https://www.soozhu.com/price/data/center/"
payload = {"act": "pricetrend", "indid": "", "csrfmiddlewaretoken": token}
r = session.post(url, data=payload)
data_json = r.json()
temp_df = pd.DataFrame(data_json["datalist"])
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.sort_values(by=["日期"], ignore_index=True, inplace=True)
return temp_df
def spot_hog_three_way_soozhu() -> pd.DataFrame:
"""
搜猪-生猪大数据-全国三元仔猪
https://www.soozhu.com/price/data/center/
:return: 全国三元仔猪
:rtype: pandas.DataFrame
"""
session = requests.session()
url = "https://www.soozhu.com/price/data/center/"
r = session.get(url)
soup = BeautifulSoup(r.text, features="lxml")
token = soup.find(name="input", attrs={"name": "csrfmiddlewaretoken"})["value"]
url = "https://www.soozhu.com/price/data/center/"
payload = {"act": "pricetrend", "indid": "4", "csrfmiddlewaretoken": token}
r = session.post(url, data=payload)
data_json = r.json()
temp_df = pd.DataFrame(data_json["datalist"])
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.sort_values(by=["日期"], ignore_index=True, inplace=True)
return temp_df
def spot_hog_crossbred_soozhu() -> pd.DataFrame:
"""
搜猪-生猪大数据-全国后备二元母猪
https://www.soozhu.com/price/data/center/
:return: 全国后备二元母猪
:rtype: pandas.DataFrame
"""
session = requests.session()
url = "https://www.soozhu.com/price/data/center/"
r = session.get(url)
soup = BeautifulSoup(r.text, features="lxml")
token = soup.find(name="input", attrs={"name": "csrfmiddlewaretoken"})["value"]
url = "https://www.soozhu.com/price/data/center/"
payload = {"act": "pricetrend", "indid": "6", "csrfmiddlewaretoken": token}
r = session.post(url, data=payload)
data_json = r.json()
temp_df = pd.DataFrame(data_json["datalist"])
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.sort_values(by=["日期"], ignore_index=True, inplace=True)
return temp_df
def spot_corn_price_soozhu() -> pd.DataFrame:
"""
搜猪-生猪大数据-全国玉米价格走势
https://www.soozhu.com/price/data/center/
:return: 全国玉米价格走势
:rtype: pandas.DataFrame
"""
session = requests.session()
url = "https://www.soozhu.com/price/data/center/"
r = session.get(url)
soup = BeautifulSoup(r.text, features="lxml")
token = soup.find(name="input", attrs={"name": "csrfmiddlewaretoken"})["value"]
url = "https://www.soozhu.com/price/data/center/"
payload = {"act": "pricetrend", "indid": "8", "csrfmiddlewaretoken": token}
r = session.post(url, data=payload)
data_json = r.json()
temp_df = pd.DataFrame(data_json["datalist"])
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.sort_values(by=["日期"], ignore_index=True, inplace=True)
return temp_df
def spot_soybean_price_soozhu() -> pd.DataFrame:
"""
搜猪-生猪大数据-全国豆粕价格走势
https://www.soozhu.com/price/data/center/
:return: 全国豆粕价格走势
:rtype: pandas.DataFrame
"""
session = requests.session()
url = "https://www.soozhu.com/price/data/center/"
r = session.get(url)
soup = BeautifulSoup(r.text, features="lxml")
token = soup.find(name="input", attrs={"name": "csrfmiddlewaretoken"})["value"]
url = "https://www.soozhu.com/price/data/center/"
payload = {"act": "pricetrend", "indid": "9", "csrfmiddlewaretoken": token}
r = session.post(url, data=payload)
data_json = r.json()
temp_df = pd.DataFrame(data_json["datalist"])
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.sort_values(by=["日期"], ignore_index=True, inplace=True)
return temp_df
def spot_mixed_feed_soozhu() -> pd.DataFrame:
"""
搜猪-生猪大数据-全国育肥猪合料(含自配料)半月走势
https://www.soozhu.com/price/data/center/
:return: 全国育肥猪合料(含自配料)半月走势
:rtype: pandas.DataFrame
"""
session = requests.session()
url = "https://www.soozhu.com/price/data/center/"
r = session.get(url)
soup = BeautifulSoup(r.text, features="lxml")
token = soup.find(name="input", attrs={"name": "csrfmiddlewaretoken"})["value"]
url = "https://www.soozhu.com/price/data/center/"
payload = {"act": "pricetrend", "indid": "11", "csrfmiddlewaretoken": token}
r = session.post(url, data=payload)
data_json = r.json()
temp_df = pd.DataFrame(data_json["datalist"])
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.sort_values(by=["日期"], ignore_index=True, inplace=True)
return temp_df
if __name__ == "__main__":
spot_hog_soozhu_df = spot_hog_soozhu()
print(spot_hog_soozhu_df)
spot_hog_year_trend_soozhu_df = spot_hog_year_trend_soozhu()
print(spot_hog_year_trend_soozhu_df)
spot_hog_lean_price_soozhu_df = spot_hog_lean_price_soozhu()
print(spot_hog_lean_price_soozhu_df)
spot_hog_three_way_soozhu_df = spot_hog_three_way_soozhu()
print(spot_hog_three_way_soozhu_df)
spot_hog_crossbred_soozhu_df = spot_hog_crossbred_soozhu()
print(spot_hog_crossbred_soozhu_df)
spot_corn_price_soozhu_df = spot_corn_price_soozhu()
print(spot_corn_price_soozhu_df)
spot_soybean_price_soozhu_df = spot_soybean_price_soozhu()
print(spot_soybean_price_soozhu_df)
spot_mixed_feed_soozhu_df = spot_mixed_feed_soozhu()
print(spot_mixed_feed_soozhu_df)
@@ -0,0 +1,121 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
"""
Date: 2024/5/16 20:00
Desc: 99 期货-数据-期现-现货走势
https://www.99qh.com/data/spotTrend
"""
import json
import pandas as pd
import requests
from bs4 import BeautifulSoup
def __get_item_of_spot_price_qh() -> pd.DataFrame:
"""
99 期货-数据-期现-品种和 ID 对应表
https://www.99qh.com/data/spotTrend
:return: 品种和 ID 对应表
:rtype: str
"""
url = "https://www.99qh.com/data/spotTrend"
r = requests.get(url)
soup = BeautifulSoup(r.text, features="lxml")
data_text = soup.find(name="script", attrs={"id": "__NEXT_DATA__"}).text
data_json = json.loads(data_text)
big_list = []
for item in data_json["props"]["pageProps"]["data"]["varietyListData"]:
big_list.extend(item["productList"])
temp_df = pd.DataFrame(big_list)
temp_df = temp_df[["qhExchangeName", "name", "productId"]]
return temp_df
def __get_token_of_spot_price_qh() -> str:
"""
99 期货-数据-期现-token
https://www.99qh.com/data/spotTrend
:return: token
:rtype: str
"""
url = "https://centerapi.fx168api.com/app/common/v.js"
headers = {
"Origin": "https://www.99qh.com",
"Referer": "https://www.99qh.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/124.0.0.0 Safari/537.36",
}
r = requests.get(url, headers=headers)
token = r.headers["_pcc"]
return token
def spot_price_table_qh() -> pd.DataFrame:
"""
99 期货-数据-期现-交易所与品种对照表
https://www.99qh.com/data/spotTrend
:return: 交易所与品种对照表
:rtype: pandas.DataFrame
"""
temp_df = __get_item_of_spot_price_qh()
temp_df.rename(
columns={
"qhExchangeName": "交易所名称",
"name": "品种名称",
},
inplace=True,
)
temp_df = temp_df[
[
"交易所名称",
"品种名称",
]
]
return temp_df
def spot_price_qh(symbol: str = "螺纹钢") -> pd.DataFrame:
"""
99 期货-数据-期现-现货走势
https://www.99qh.com/data/spotTrend
:param symbol: 品种名称
:type symbol: str
:return: 现货走势
:rtype: pandas.DataFrame
"""
inner_df = __get_item_of_spot_price_qh()
symbol_map = dict(zip(inner_df["name"], inner_df["productId"]))
url = "https://centerapi.fx168api.com/app/qh/api/spot/trend"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/124.0.0.0 Safari/537.36",
"_pcc": __get_token_of_spot_price_qh(),
"Origin": "https://www.99qh.com",
"Referer": "https://www.99qh.com",
}
params = {
"productId": symbol_map[symbol],
"pageNo": "1",
"pageSize": "50000",
"startDate": "",
"endDate": "2050-01-01",
"appCategory": "web",
}
r = requests.get(url, params=params, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["list"])
temp_df.rename(
columns={"date": "日期", "fp": "期货收盘价", "sp": "现货价格"}, inplace=True
)
temp_df.sort_values(by=["日期"], 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")
return temp_df
if __name__ == "__main__":
spot_price_qh_df = spot_price_qh(symbol="螺纹钢")
print(spot_price_qh_df)
@@ -0,0 +1,243 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
"""
Date: 2025/4/11 22:00
Desc: 上海黄金交易所-数据资讯-行情走势
https://www.sge.com.cn/sjzx/mrhq
上海黄金交易所-数据资讯-上海金基准价-历史数据
上海黄金交易所-数据资讯-上海银基准价-历史数据
"""
import pandas as pd
import requests
from akshare.utils.cons import headers
def spot_symbol_table_sge() -> pd.DataFrame:
"""
上海黄金交易所-数据资讯-行情走势-品种表
https://www.sge.com.cn/sjzx/mrhq
:return: 品种表
:rtype: pandas.DataFrame
"""
temp_list = [
"Au99.99",
"Au99.95",
"Au100g",
"Pt99.95",
"Ag(T+D)",
"Au(T+D)",
"mAu(T+D)",
"Au(T+N1)",
"Au(T+N2)",
"Ag99.99",
"iAu99.99",
"Au99.5",
"iAu100g",
"iAu99.5",
"PGC30g",
"NYAuTN06",
"NYAuTN12",
]
temp_df = pd.DataFrame(temp_list)
temp_df.reset_index(inplace=True)
temp_df.columns = ["序号", "品种"]
temp_df["序号"] = temp_df["序号"] + 1
return temp_df
def spot_quotations_sge(symbol: str = "Au99.99") -> pd.DataFrame:
"""
上海黄金交易所-实时行情数据
https://www.sge.com.cn/
https://www.sge.com.cn/graph/quotations
:param symbol: choice of {'Au99.99', 'Au99.95', 'Au100g', 'Pt99.95', 'Ag(T+D)', 'Au(T+D)',
'mAu(T+D)', 'Au(T+N1)', 'Au(T+N2)', 'Ag99.99', 'iAu99.99', 'Au99.5', 'iAu100g',
'iAu99.5', 'PGC30g', 'NYAuTN06', 'NYAuTN12'}; 可以通过 ak.spot_symbol_table_sge() 获取品种表
:type symbol: str
:return: 行情数据
:rtype: pandas.DataFrame
"""
url = "https://www.sge.com.cn/graph/quotations"
payload = {"instid": symbol}
headers = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Length": "15",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "www.sge.com.cn",
"Origin": "https://www.sge.com.cn",
"Pragma": "no-cache",
"Referer": "https://www.sge.com.cn/",
"sec-ch-ua": '"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/107.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
}
r = requests.get(url, data=payload, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(
{
"品种": data_json["heyue"],
"时间": data_json["times"],
"现价": data_json["data"],
"更新时间": data_json["delaystr"],
}
)
temp_df["现价"] = pd.to_numeric(temp_df["现价"], errors="coerce")
# 将更新时间中的时间部分提取出来
update_time = temp_df["更新时间"].iloc[0].split()[1]
# 将时间列转换为时间格式以便排序
temp_df["时间"] = pd.to_datetime(temp_df["时间"], format="%H:%M").dt.time
# 过滤掉大于等于更新时间的数据
temp_df = temp_df[temp_df["时间"].astype(str) < update_time]
# 按时间排序
temp_df = temp_df.sort_values(by=["时间"])
temp_df.reset_index(inplace=True, drop=True)
return temp_df
def spot_hist_sge(symbol: str = "Au99.99") -> pd.DataFrame:
"""
上海黄金交易所-数据资讯-行情走势-历史数据
https://www.sge.com.cn/sjzx/mrhq
:param symbol: choice of {'Au99.99', 'Au99.95', 'Au100g', 'Pt99.95', 'Ag(T+D)', 'Au(T+D)',
'mAu(T+D)', 'Au(T+N1)', 'Au(T+N2)', 'Ag99.99', 'iAu99.99', 'Au99.5', 'iAu100g', 'iAu99.5',
'PGC30g', 'NYAuTN06', 'NYAuTN12'}; 可以通过 ak.spot_symbol_table_sge() 获取品种表
:type symbol: str
:return: 历史数据
:rtype: pandas.DataFrame
"""
url = "https://www.sge.com.cn/graph/Dailyhq"
payload = {"instid": symbol}
headers = {
"Accept": "text/html, */*; q=0.01",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Content-Length": "15",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "www.sge.com.cn",
"Origin": "https://www.sge.com.cn",
"Pragma": "no-cache",
"Referer": "https://www.sge.com.cn/sjzx/mrhq",
"sec-ch-ua": '"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/107.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
}
r = requests.post(url, data=payload, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(data_json["time"])
temp_df.columns = [
"date",
"open",
"close",
"low",
"high",
]
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")
return temp_df
def spot_golden_benchmark_sge() -> pd.DataFrame:
"""
上海黄金交易所-数据资讯-上海金基准价-历史数据
https://www.sge.com.cn/sjzx/jzj
:return: 历史数据
:rtype: pandas.DataFrame
"""
url = "https://www.sge.com.cn/graph/DayilyJzj"
payload = {}
r = requests.post(url, data=payload, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(data_json["wp"])
temp_df.columns = [
"交易时间",
"晚盘价",
]
temp_df["交易时间"] = pd.to_datetime(temp_df["交易时间"], unit="ms").dt.date
temp_zp_df = pd.DataFrame(data_json["zp"])
temp_zp_df.columns = [
"交易时间",
"早盘价",
]
temp_zp_df["交易时间"] = pd.to_datetime(
temp_zp_df["交易时间"], unit="ms", errors="coerce"
).dt.date
temp_df["早盘价"] = temp_zp_df["早盘价"]
temp_df["晚盘价"] = pd.to_numeric(temp_df["晚盘价"], errors="coerce")
temp_df["早盘价"] = pd.to_numeric(temp_df["早盘价"], errors="coerce")
return temp_df
def spot_silver_benchmark_sge() -> pd.DataFrame:
"""
上海黄金交易所-数据资讯-上海银基准价-历史数据
https://www.sge.com.cn/sjzx/mrhq
:return: 历史数据
:rtype: pandas.DataFrame
"""
url = "https://www.sge.com.cn/graph/DayilyShsilverJzj"
payload = {}
r = requests.post(url, data=payload, headers=headers)
data_json = r.json()
temp_df = pd.DataFrame(data_json["wp"])
temp_df.columns = [
"交易时间",
"晚盘价",
]
temp_df["交易时间"] = pd.to_datetime(temp_df["交易时间"], unit="ms").dt.date
temp_zp_df = pd.DataFrame(data_json["zp"])
temp_zp_df.columns = [
"交易时间",
"早盘价",
]
temp_zp_df["交易时间"] = pd.to_datetime(
temp_zp_df["交易时间"], unit="ms", errors="coerce"
).dt.date
temp_df["早盘价"] = temp_zp_df["早盘价"]
temp_df["晚盘价"] = pd.to_numeric(temp_df["晚盘价"], errors="coerce")
temp_df["早盘价"] = pd.to_numeric(temp_df["早盘价"], errors="coerce")
return temp_df
if __name__ == "__main__":
spot_symbol_table_sge_df = spot_symbol_table_sge()
print(spot_symbol_table_sge_df)
spot_hist_sge_df = spot_hist_sge(symbol="Au99.99")
print(spot_hist_sge_df)
spot_golden_benchmark_sge_df = spot_golden_benchmark_sge()
print(spot_golden_benchmark_sge_df)
spot_silver_benchmark_sge_df = spot_silver_benchmark_sge()
print(spot_silver_benchmark_sge_df)
for spot in spot_symbol_table_sge_df["品种"].tolist():
spot_hist_sge_df = spot_hist_sge(symbol=spot)
print(spot_hist_sge_df)
spot_quotations_sge_df = spot_quotations_sge(symbol="Au99.99")
print(spot_quotations_sge_df)