【问题标题】:Write multi-line data to CSV in python在python中将多行数据写入CSV
【发布时间】:2021-10-23 13:27:51
【问题描述】:

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

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 多个基金的数据。我会做更多的实验,任何问题可能会在稍后的另一个 Q :) 由于我是新手,任何其他改进以及为什么你会这样做也将不胜感激!

【问题讨论】:

  • 可以分享网址吗?

标签: python python-3.x csv web-scraping beautifulsoup


【解决方案1】:

使用 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2012-12-17
    相关资源
    最近更新 更多