您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:Python批量获取基金数据的方法步骤

51自学网 2021-10-30 22:53:16
  python
这篇教程Python批量获取基金数据的方法步骤写得很实用,希望能帮到您。

20年初准备投资基金,想爬取基金的业绩数据。

20年基金迎来了爆发式增长,现把代码开源以供参考。

本代码只能实现初步汇总,输出csv文件来保存基金的单位&累计净值,后期仍需要结合统计方法来筛选优质基金。

参考了网上的部分代码,实在不记得出处了,侵删。

import requestsimport timeimport execjsstart = time.perf_counter()# 获取所有基金编号def getAllCode():  url = 'http://fund.eastmoney.com/js/fundcode_search.js'  content = requests.get(url)  jsContent = execjs.compile(content.text)  rawData = jsContent.eval('r')  allCode = []  for code in rawData:    allCode.append(code[0])  return allCodeallCode = getAllCode()del allCode[100:len(allCode)]# print(len(allCode))# 获取基金编号为fscode的所有信息def getUrl(fscode):  head = 'http://fund.eastmoney.com/pingzhongdata/'  tail = '.js?v=' + time.strftime("%Y%m%d%H%M%S", time.localtime())  return head + fscode + tail# 获取净值def getWorth(fscode):  content = requests.get(getUrl(fscode))  jsContent = execjs.compile(content.text)  name = jsContent.eval('fS_name')  code = jsContent.eval('fS_code')  # 单位净值走势  netWorthTrend = jsContent.eval('Data_netWorthTrend')  # 累计净值走势  ACWorthTrend = jsContent.eval('Data_ACWorthTrend')  # 近一年收益率  Profit_12month = jsContent.eval('syl_1n')  netWorth = []  ACWorth = []  for dayWorth in netWorthTrend[::-1]:    netWorth.append(dayWorth['y'])  for dayACWorth in ACWorthTrend[::-1]:    ACWorth.append(dayACWorth[1])  print(name, code)  return netWorth, ACWorthnetWorthFile = open('./netWorth.csv', 'w')ACWorthFile = open('./ACWorth.csv', 'w')for code in allCode:  try:    netWorth, ACWorth = getWorth(code)  except:    continue  if len(netWorth) <= 0 or len(ACWorth) < 0:    # print(code + " empty data")    continue  netWorthFile.write("/'" + code + "/',")  netWorthFile.write(",".join(list(map(str, netWorth))))  netWorthFile.write("/n")  ACWorthFile.write("/'" + code + "/',")  ACWorthFile.write(",".join(list(map(str, ACWorth))))  ACWorthFile.write("/n")  # print("write " + code + " success.")netWorthFile.close()ACWorthFile.close()end = time.perf_counter()print('Running time: %s seconds' %(end-start))

到此这篇关于Python批量获取基金数据的方法步骤的文章就介绍到这了,更多相关Python批量获取基金数据内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net! 


Python对Excel进行处理的实操指南
python中的bool数组取反案例
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。