简体   繁体   English

python中向CSV写入多行数据

[英]Write multi-line data to CSV in python

I'm a noob trying to learn Python by scraping a website to track fund parameters.我是一个菜鸟,试图通过抓取网站来跟踪基金参数来学习 Python。 So far, the following code isolates and shows the data that I need,到目前为止,以下代码隔离并显示了我需要的数据,

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()

This code shows the following data:此代码显示以下数据:

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

How can I write this data to a CSV with the following headers?如何使用以下标头将此数据写入 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

This is for a single fund and I need to get this data for about 100 more funds.这是针对单个基金的,我需要为大约 100 个以上的基金获取此数据。 I will experiment more and any issues there are perhaps for another Q at a later time:) Since I'm a newbie, any other improvements and why you'd do those would also be appreciated!我将进行更多实验,并且可能会在以后的另一个问题中遇到任何问题:)由于我是新手,任何其他改进以及您为什么要这样做的原因也将不胜感激!

Assemble the data for each fund in a list to easily write it out in CSV format using Python's builtin csv module :使用 Python 的内置csv 模块,将每个基金的数据组合成一个列表,以 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM