20年初准备投资基金,想爬取基金的业绩数据。
20年基金迎来了爆发式增长,现把代码开源以供参考。
本代码只能实现初步汇总,输出csv文件来保存基金的单位&累计净值,后期仍需要结合统计方法来筛选优质基金。
参考了网上的部分代码,实在不记得出处了,侵删。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
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批量获取基金数据的方法步骤的文章就介绍到这了,更多相关Python批量获取基金数据内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/Coefficient1996/article/details/114360549