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("\'" + 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))

猜你喜欢

转载自blog.csdn.net/Coefficient1996/article/details/114360549