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))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/266677.html
標籤:python
