本篇文章给大家分享的是有关使用Python怎么批量获取基金数据,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
Python是一种编程语言,内置了许多有效的工具,Python几乎无所不能,该语言通俗易懂、容易入门、功能强大,在许多领域中都有广泛的应用,例如最热门的大数据分析,人工智能,Web开发等。
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("\'" + 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怎么批量获取基金数据,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。