type 类型,历史净值用lsjz表示
code 基金代码,六位数字
sdate 开始日期,格式是yyyy-mm-dd
edate 结束日期,格式是yyyy-mm-dd
per 一页显示多少条记录
为了便于分析页面数据,要保证所选择日期范围内的净值在一个页面全部显示,可以把per设成很大的值,比如65535。
返回的页面数据比较简单,只有一个历史净值的表格和总记录数,总页数和当前页数。
var apidata={ content:"
净值日期 单位净值 累计净值 日增长率 申购状态 赎回状态 分红送配
2018-03-02 2.3580 2.3580 0.17% 开放申购 开放赎回
2018-03-01 2.3540 2.3540 0.56% 开放申购 开放赎回
2018-02-28 2.3410 2.3410 -1.35% 开放申购 开放赎回
2018-02-27 2.3730 2.3730 -2.06% 开放申购 开放赎回
2018-02-26 2.4230 2.4230 0.29% 开放申购 开放赎回
2018-02-23 2.4160 2.4160 -0.49% 开放申购 开放赎回
2018-02-22 2.4280 2.4280 2.58% 开放申购 开放赎回
",records:7,pages:1,curpage:1};
html.PNG
用BeautifulSoup库的findAll找到tbody(表格主体)标签,然后在里面找tr(表格中的一行)标签,单元格内容是:
td:nth-of-type(1)(第1个单元格)是净值日期
td:nth-of-type(2)(第2个单元格)是单位净值
td:nth-of-type(3)(第3个单元格)是累计净值
td:nth-of-type(4)(第4个单元格)是日增长率
范例代码如下:
# -*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup
from prettytable import *
def get_url(url, params=None, proxies=None):
rsp = requests.get(url, params=params, proxies=proxies)
rsp.raise_for_status()
return rsp.text
def get_fund_data(code, start='', end=''):
record = {'Code': code}
url = 'http://fund.eastmoney.com/f10/F10DataApi.aspx'
params = {'type': 'lsjz', 'code': code, 'page': 1, 'per': 65535, 'sdate': start, 'edate': end}
html = get_url(url, params)
soup = BeautifulSoup(html, 'html.parser')
records = []
tab = soup.findAll('tbody')[0]
for tr in tab.findAll('tr'):
if tr.findAll('td') and len((tr.findAll('td'))) == 7:
record['Date'] = str(tr.select('td:nth-of-type(1)')[0].getText().strip())
record['NetAssetValue'] = str(tr.select('td:nth-of-type(2)')[0].getText().strip())
record['ChangePercent'] = str(tr.select('td:nth-of-type(4)')[0].getText().strip())
records.append(record.copy())
return records
def demo(code, start, end):
table = PrettyTable()
table.field_names = ['Code', 'Date', 'NAV', 'Change']
table.align['Change'] = 'r'
records = get_fund_data(code, start, end)
for record in records:
table.add_row([record['Code'], record['Date'], record['NetAssetValue'], record['ChangePercent']])
return table
if __name__ == "__main__":
print demo('110022', '2018-02-22', '2018-03-02')
输出结果如下:
+--------+------------+--------+--------+
| Code | Date | NAV | Change |
+--------+------------+--------+--------+
| 110022 | 2018-03-02 | 2.3580 | 0.17% |
| 110022 | 2018-03-01 | 2.3540 | 0.56% |
| 110022 | 2018-02-28 | 2.3410 | -1.35% |
| 110022 | 2018-02-27 | 2.3730 | -2.06% |
| 110022 | 2018-02-26 | 2.4230 | 0.29% |
| 110022 | 2018-02-23 | 2.4160 | -0.49% |
| 110022 | 2018-02-22 | 2.4280 | 2.58% |
+--------+------------+--------+--------+