美文网首页
python网络爬虫10:爬取基金净值数据2

python网络爬虫10:爬取基金净值数据2

作者: 0清婉0 | 来源:发表于2021-01-20 21:16 被阅读0次

结合上次的学习,这次加个折线图,不过还没研究出如何在图上加年份。

import requests

import time

import execjs

import matplotlib.pyplot as plt

def getUrl(fscode):

    head = 'http://fund.eastmoney.com/pingzhongdata/'

    tail = '.js?v=' + time.strftime('%Y%m%d%H%M%S')

    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')  # 累计净值走势数据

    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

netWorth, ACWorth = getWorth('007119')

print(netWorth)

plt.figure(figsize=(10,5))

plt.plot(netWorth[:60][::-1])

plt.show()

相关文章

网友评论

      本文标题:python网络爬虫10:爬取基金净值数据2

      本文链接:https://www.haomeiwen.com/subject/edrlzktx.html