日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python爬取基金历史净值_Python爬取天天基金网历史净值数据

發(fā)布時(shí)間:2023/12/31 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬取基金历史净值_Python爬取天天基金网历史净值数据 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

type 類型,歷史凈值用lsjz表示

code 基金代碼,六位數(shù)字

sdate 開始日期,格式是yyyy-mm-dd

edate 結(jié)束日期,格式是yyyy-mm-dd

per 一頁(yè)顯示多少條記錄

為了便于分析頁(yè)面數(shù)據(jù),要保證所選擇日期范圍內(nèi)的凈值在一個(gè)頁(yè)面全部顯示,可以把per設(shè)成很大的值,比如65535。

返回的頁(yè)面數(shù)據(jù)比較簡(jiǎn)單,只有一個(gè)歷史凈值的表格和總記錄數(shù),總頁(yè)數(shù)和當(dāng)前頁(yè)數(shù)。

var apidata={ content:"

凈值日期 單位凈值 累計(jì)凈值 日增長(zhǎng)率 申購(gòu)狀態(tài) 贖回狀態(tài) 分紅送配

2018-03-02 2.3580 2.3580 0.17% 開放申購(gòu) 開放贖回

2018-03-01 2.3540 2.3540 0.56% 開放申購(gòu) 開放贖回

2018-02-28 2.3410 2.3410 -1.35% 開放申購(gòu) 開放贖回

2018-02-27 2.3730 2.3730 -2.06% 開放申購(gòu) 開放贖回

2018-02-26 2.4230 2.4230 0.29% 開放申購(gòu) 開放贖回

2018-02-23 2.4160 2.4160 -0.49% 開放申購(gòu) 開放贖回

2018-02-22 2.4280 2.4280 2.58% 開放申購(gòu) 開放贖回

",records:7,pages:1,curpage:1};

html.PNG

用BeautifulSoup庫(kù)的findAll找到tbody(表格主體)標(biāo)簽,然后在里面找tr(表格中的一行)標(biāo)簽,單元格內(nèi)容是:

td:nth-of-type(1)(第1個(gè)單元格)是凈值日期

td:nth-of-type(2)(第2個(gè)單元格)是單位凈值

td:nth-of-type(3)(第3個(gè)單元格)是累計(jì)凈值

td:nth-of-type(4)(第4個(gè)單元格)是日增長(zhǎng)率

范例代碼如下:

# -*- 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')

輸出結(jié)果如下:

+--------+------------+--------+--------+

| 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% |

+--------+------------+--------+--------+

總結(jié)

以上是生活随笔為你收集整理的python爬取基金历史净值_Python爬取天天基金网历史净值数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。