Python批量获取基金数据的方法步骤

这篇文章主要介绍了Python批量获取基金数据的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

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

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

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

 import requests import time import execjs start = 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 allCode allCode = 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, ACWorth netWorthFile = 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("\'" acworthfile.write(",".join(list(map(str, acworth)))) acworthfile.write("\n") print("write success.") networthfile.close() acworthfile.close() end=time.perf_counter() print('running time: %s seconds' %(end-start)) < pre>

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

以上就是Python批量获取基金数据的方法步骤的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python