网站首页 > 技术文章 正文
获取某基金网站估值原始数据
postman天天基金网接口模拟
python代码实现
import requests
url = "http://fundgz.1234567.com.cn/js/006030.js?rt=1612781131"
payload = {}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
其中006030为基金代码
2.使用python解析返回值,将jsonp格式数据转化为json数据
_jsonp_begin = r'jsonpgz('_jsonp_end = r');'def from_jsonp(jsonp_str):
jsonp_str = jsonp_str.strip()
if not jsonp_str.startswith(_jsonp_begin) or \
not jsonp_str.endswith(_jsonp_end):
raise ValueError('Invalid JSONP')
return json.loads(jsonp_str[len(_jsonp_begin):-len(_jsonp_end)])
这里使用正则匹配返回值jsonp数据,使用json.loads方法转字典
3.以json的形式输出返回值
def get_single_data(f_code):
millis = int(round(time.time() * 1000))
try:
base_url = "http://fundgz.1234567.com.cn/js/{0}.js?rt={1}".format(f_code, millis)
response = requests.get(base_url)
resp = from_jsonp(response.text)
return True, resp
except Exception as e:
print(base_url, response.text, str(e))
return False, None
返回字段含义解析
字段名 | 中文含义 | 示例 |
fundcode | 基金唯一编码 | 006030 |
name | 基金名字 | 南方昌元转债A |
jzrq | 净值日期,即dwjz对应的日期 | 2023-08-03 |
dwjz | 单位净值,即前一交易日净值 | 1.5395 |
gsz | 估算值,即实时估值 | 1.5476 |
gszzl | 估算增长率 | 0.53 |
gztime | 估值时间 | 2023-08-04 15:00 |
注意事项:
QDII无法获取估算净值数据
完整代码:
import json
import time
import requests
_jsonp_begin = r'jsonpgz('_jsonp_end = r');'def from_jsonp(jsonp_str):
jsonp_str = jsonp_str.strip()
if not jsonp_str.startswith(_jsonp_begin) or \
not jsonp_str.endswith(_jsonp_end):
raise ValueError('Invalid JSONP')
return json.loads(jsonp_str[len(_jsonp_begin):-len(_jsonp_end)])
# 爬虫获取实时数据def get_single_data(f_code):
millis = int(round(time.time() * 1000))
try:
base_url = "http://fundgz.1234567.com.cn/js/{0}.js?rt={1}".format(f_code, millis)
response = requests.get(base_url)
resp = from_jsonp(response.text)
return True, resp
except Exception as e:
print(base_url, response.text, str(e))
return False, Noneif __name__ == '__main__':
status, fund_json_data = get_single_data("006030")
if not status:
exit(1)
print(fund_json_data)
输出结果:
{
'fundcode': '006030',
'name': '南方昌元转债A',
'jzrq': '2023-08-03',
'dwjz': '1.5395',
'gsz': '1.5476',
'gszzl': '0.53',
'gztime': '2023-08-04 15:00'
}
应用场景:
- 获取实时估算收益;
- 做量化交易;
- 上一篇: 9-Python自定义函数
- 下一篇: Python 函数,一文掌握
猜你喜欢
- 2025-03-13 python基础函数
- 2025-03-13 Python 中的海象运算符 (:=)
- 2025-03-13 AI 编程指南
- 2025-03-13 有趣的Swift特性之:多个返回值
- 2025-03-13 2-Python注释
- 2025-03-13 python中zip()函数
- 2025-03-13 Python | range()详解
- 2025-03-13 高阶Python|返回类型提示技巧 (1)
- 2025-03-13 Python 函数,一文掌握
- 2025-03-13 9-Python自定义函数
- 265℃Python短文,Python中的嵌套条件语句(六)
- 264℃python笔记:for循环嵌套。end=""的作用,图形打印
- 263℃PythonNet:实现Python与.Net代码相互调用!
- 259℃Python实现字符串小写转大写并写入文件
- 257℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 117℃原来2025是完美的平方年,一起探索六种平方的算吧
- 98℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 90℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)