本篇文章給大家分享的是有關使用Python怎么批量獲取基金數據,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

Python是一種編程語言,內置了許多有效的工具,Python幾乎無所不能,該語言通俗易懂、容易入門、功能強大,在許多領域中都有廣泛的應用,例如最熱門的大數據分析,人工智能,Web開發等。
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怎么批量獲取基金數據-創新互聯
網頁鏈接:http://www.yijiale78.com/article32/djjspc.html
成都網站建設公司_創新互聯,為您提供企業建站、網站改版、外貿建站、面包屑導航、關鍵詞優化、軟件開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯