在python中将多行数据写入CSV

2024-04-26 08:07:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个试图通过抓取一个网站来跟踪基金参数来学习Python的noob。到目前为止,以下代码隔离并显示了我需要的数据

from bs4 import BeautifulSoup
import requests

source = requests.get('https://www.fundaggregatorurl.com/path/to/fund').text
soup = BeautifulSoup(source, 'lxml')

# print(soup.prettify())

print("\n1Y growth rate vs S&P BSE 500 TRI\n")
# Pinpoints the 1Y growth rate of the scheme and the S&P BSE 500 TRI
for snippet in soup.find_all('div', class_='scheme_per_amt prcntreturn 1Y'):
    print(snippet.text.lstrip())

print("\nNAV, AUM and Expense Ratio\n")
# Pinpoints NAV, AUM and Expense Ratio
for snippet in soup.find_all('span', class_='amt'):
    print(snippet.text)

# Get the risk analysis data
source = requests.get('https://www.fundaggregatorurl.com/path/to/fund/riskanalysis').text
soup = BeautifulSoup(source, 'lxml')

print("\nRisk Ratios\n")
# Pinpoints NAV, AUM and Expense Ratio
for snippet in soup.find_all('div', class_='percentage'):
    split_data = snippet.text.split('vs')
    print(*split_data, sep=" ")

print()

此代码显示以下数据:

1Y growth rate vs S&P BSE 500 TRI

68.83%
50.85%

NAV, AUM and Expense Ratio

185.9414
2704.36
1.5%

Risk Ratios

19.76 17.95
0.89 0.93
0.77 0.72
0.17 0.14
4.59 2.32

如何使用以下标题将此数据写入CSV

Fund growth         Category Growth         Current NAV         AUM                 Expense Ratio           Fund std dev            Category std dev            Fund beta           Category beta           Fund Sharpe ratio           Category Sharpe ratio           Fund Treynor's ratio            Category Treynor's Ratio            Fund Jension's Alpha            Category Jension's Alpha
68.83%              50.85%                  185.9414            2704.36             1.5%                    19.76                   17.95                       0.89                0.93                    0.77                        0.72                            0.17                            0.14                                4.59                            2.32

这是一个单一的基金,我需要得到大约100多个基金的数据。我会做更多的实验,任何问题可能会在以后的时间再问:)因为我是一个新手,任何其他的改进和你为什么会这样做也将不胜感激


1条回答
网友
1楼 · 发布于 2024-04-26 08:07:22

使用Python的内置csv module将每个基金的数据组合在一个列表中,以CSV格式轻松写出:

import csv

funds = ['fund1', 'fund2']
# the header should match the number of data items
header = ['Fund growth', 'Category Growth', 'Current NAV', 'AUM']

with open('funds.csv', 'w', newline='') as csvfile:
    fund_writer = csv.writer(csvfile)
    fund_writer.writerow(header)
    for fund in funds:
        fund_data = []
        source = requests.get('https://www.fundaggregatorurl.com/path/to/' + fund).text
        soup = BeautifulSoup(source, 'lxml')

        for snippet in soup.find_all('div', class_='scheme_per_amt prcntreturn 1Y'):
            fund_data.append(snippet.text.lstrip())

        # do remaining parsing...

        fund_writer.writerow(fund_data)

相关问题 更多 >