Method Steps of Obtaining Fund Data in Batch by Python
- 2021-09-20 20:51:17
- OfStack
At the beginning of 20 years, I prepared to invest in the fund and wanted to crawl the performance data of the fund.
In the past 20 years, the fund has experienced explosive growth, and now the code is open source for reference.
This code can only achieve preliminary summary, output csv file to save the unit of the fund & Accumulated net value, it is still necessary to combine statistical methods to screen high-quality funds in the later period.
Refer to some codes on the Internet, but I really don't remember the source, so I invaded and deleted it.
import requests
import time
import execjs
start = time.perf_counter()
# Get all fund numbers
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))
# The Acquisition Fund number is fscode All the information of
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
# Acquisition of net worth
def getWorth(fscode):
content = requests.get(getUrl(fscode))
jsContent = execjs.compile(content.text)
name = jsContent.eval('fS_name')
code = jsContent.eval('fS_code')
# Trend of unit net value
netWorthTrend = jsContent.eval('Data_netWorthTrend')
# Trend of cumulative net value
ACWorthTrend = jsContent.eval('Data_ACWorthTrend')
# Near 1 Annual rate of return
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))