理财有风险,投资需谨慎
适用基金
股票型基金(高风险,股票占基金持仓比超过90%)
实现步骤
1、先选一支基金:大摩进取优选股票
2、在天天基金网中对应定位到它的前十大重仓股列表信息相关的两个URL https://fundf10.eastmoney.com/FundArchivesDatas.aspx?type=jjcc&code=000594&topline=10&year=&month=&rt=0.8679339233381893
https://push2.eastmoney.com/api/qt/ulist.np/get?cb=jQuery18306519919520568553_1691590540061&fltt=2&invt=2&ut=267f9ad526dbe6b0262ab19316f5a25b&fields=f3,f12,f14&secids=1.601689,0.002594,0.002129,1.600563,0.301029,1.688012,1.688777,1.688111,1.603737,0.300408&_=1691590541220
3、通过爬取数据与解析,获取这十大重仓股票的信息:代码、占净值比例、实时涨跌幅
4、通过估算获取估值,这个估值计算公式 sum(占净值比例*实时涨跌幅),
由于这个公式目前比较简单粗暴,且前十重仓股的占比情况各不相同,因此公式还有很大优化空间,但基于这个还是可以较为准确地预估基金的瞬时涨跌趋势和涨跌幅了。
废话不多说,直接上可运行的代码,如果还有具体问题可以进一步提问哦
5、可运行源码(需要将HEADERS的cookie值“xx”修改为自己的cookie)
import html
import json
import requests
from bs4 import BeautifulSoup
item = {
"code": "000594", "name": "大摩进取优选股票",
"socket_info_url": "http://fundf10.eastmoney.com/FundArchivesDatas.aspx?type=jjcc&code=000594&topline=10&year=&month=&rt=0.8679339233381893",
"socket_price_url": "https://push2.eastmoney.com/api/qt/ulist.np/get?cb=jQuery18306519919520568553_1691590540061&fltt=2&invt=2&ut=267f9ad526dbe6b0262ab19316f5a25b&fields=f3,f12,f14&secids=1.601689,0.002594,0.002129,1.600563,0.301029,1.688012,1.688777,1.688111,1.603737,0.300408&_=1691590541220"
}
HEADERS = {
'accept': '*/*',
'accept-encoding': 'gzip, deflate',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,fr;q=0.7,zh-TW;q=0.6',
'cookie': 'xxx',
'referer': 'http://fundf10.eastmoney.com/',
'sec-fetch-mode': 'cors',
"sec-fetch-site": 'same-origin',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}
def cal_price(data):
price = 0
for item in data:
percent = round(float(item["percent"][:-1])/100,4)
if item["current_price"] != "-":
price += item["current_price"] * percent
return round(price, 2)
def cal_current_price(socket_info_url, socket_price_url):
socket_info_res = requests.get(socket_info_url, headers=HEADERS)
socket_info_temp_res = socket_info_res.text.split('"')[1]
socket_info_soup = BeautifulSoup(html.unescape(socket_info_temp_res), 'html.parser')
socket_price_res = requests.get(socket_price_url, headers=MAC_HEADERS)
socket_price_data = json.loads(socket_price_res.text.split("(")[1].split(")")[0])
sockets = []
for i in range(1, 11):
try:
item = {
"scode": socket_info_soup.select(f"table > tbody > tr:nth-child({i}) > td:nth-child(2)")[0].text,
"name": socket_info_soup.select(f"table > tbody > tr:nth-child({i}) > td:nth-child(3)")[0].text,
"percent": socket_info_soup.select(f"table > tbody > tr:nth-child({i}) > td:nth-child(7)")[0].text
}
sockets.append(item)
except Exception as e:
print(e)
try:
for i, item in enumerate(socket_price_data["data"]["diff"]):
sockets[i].update({"current_price": item.get("f3", 0)})
except Exception as e:
print(e)
print(socket_price_data["data"]["diff"])
return cal_price(sockets)
ret = cal_current_price(item["socket_info_url"], item["socket_price_url"])
print(ret)
运行结果