Python批量获取基金数据的方法步骤
20年初准备投资基金,想爬取基金的业绩数据。
20年基金迎来了爆发式增长,现把代码开源以供参考。
本代码只能实现初步汇总,输出csv文件来保存基金的单位&累计净值,后期仍需要结合统计方法来筛选优质基金。
参考了网上的部分代码,实在不记得出处了,侵删。
importrequests
importtime
importexecjs
start=time.perf_counter()
#获取所有基金编号
defgetAllCode():
url='http://fund.eastmoney.com/js/fundcode_search.js'
content=requests.get(url)
jsContent=execjs.compile(content.text)
rawData=jsContent.eval('r')
allCode=[]
forcodeinrawData:
allCode.append(code[0])
returnallCode
allCode=getAllCode()
delallCode[100:len(allCode)]
#print(len(allCode))
#获取基金编号为fscode的所有信息
defgetUrl(fscode):
head='http://fund.eastmoney.com/pingzhongdata/'
tail='.js?v='+time.strftime("%Y%m%d%H%M%S",time.localtime())
returnhead+fscode+tail
#获取净值
defgetWorth(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=[]
fordayWorthinnetWorthTrend[::-1]:
netWorth.append(dayWorth['y'])
fordayACWorthinACWorthTrend[::-1]:
ACWorth.append(dayACWorth[1])
print(name,code)
returnnetWorth,ACWorth
netWorthFile=open('./netWorth.csv','w')
ACWorthFile=open('./ACWorth.csv','w')
forcodeinallCode:
try:
netWorth,ACWorth=getWorth(code)
except:
continue
iflen(netWorth)<=0orlen(ACWorth)<0:
#print(code+"emptydata")
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('Runningtime:%sseconds'%(end-start))
到此这篇关于Python批量获取基金数据的方法步骤的文章就介绍到这了,更多相关Python批量获取基金数据内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短