yahoofinancialsA powerful financial data module used for pulling data from Yahoo Finance. This module can pull fundamental and technical data for stocks, indexes, currencies, cryptos, ETFs, Mutual Funds, U.S. Treasuries, and commodity futures.项目地址:https://gitcode.com/gh_mirrors/ya/yahoofinancials
在 yahoofinancials
仓库中,主要的目录结构如下:
src/
: 这是源代码目录,包含了 yahoofinancials.py
主要模块和其他辅助脚本。examples/
: 示例代码目录,用于演示如何使用该库进行股票数据获取。tests/
: 单元测试目录,用来验证代码功能的正确性。requirements.txt
: 列出了项目的依赖包。README.md
: 项目说明文件,提供了简要的项目介绍和安装指南。LICENSE
: 许可证文件,定义了该项目的授权条款。yahoofinancials.py
是这个库的主要模块,它定义了 YahooFinancials
类。该类是整个库的核心,通过实例化这个类并调用其方法,可以获取到股票相关的财务数据和价格历史数据。
例如,以下代码展示了如何创建一个 YahooFinancials
实例并获取苹果公司(AAPL)的年度财务报表数据:
from yahoofinancials import YahooFinancials
ticker = 'AAPL'
yahoo_financials = YahooFinancials(ticker)
annual_balance_sheet = yahoo_financials.get_financial_stmts('annual', 'balance')
print(annual_balance_sheet)
这里的 get_financial_stmts()
方法接收两个参数:报告周期(如 'annual'
或 'quarterly'
)和报表类型(如 'income'
,'balance'
等)。
yahoofinancials
库本身没有独立的配置文件,但你可以通过设置 Python 的全局变量或传递参数给库的方法来改变其行为。例如,使用 concurrent=True
和 max_workers=n
可以并发地处理多个股票数据请求,而 proxies
参数则可以设置代理服务器地址,以便在受限网络环境中使用。
以下是设置并发请求的例子:
from yahoofinancials import YahooFinancials
tickers = ['AAPL', 'GOOG']
yahoo_financials = YahooFinancials(tickers, concurrent=True, max_workers=8)
# ... 接着执行数据获取操作 ...
此外,若需针对非美国地区的股市,可以通过 country
参数指定国家代码:
from yahoofinancials import YahooFinancials
ticker = 'AAPL'
yahoo_financials = YahooFinancials(ticker, country='CA')
# ... 接着执行数据获取操作 ...
请注意,尽管库不直接提供配置文件,但在实际应用中,您可以创建自己的配置文件(如 .yaml
或 .json
),然后加载这些设置以影响 YahooFinancials
对象的行为。
yahoofinancialsA powerful financial data module used for pulling data from Yahoo Finance. This module can pull fundamental and technical data for stocks, indexes, currencies, cryptos, ETFs, Mutual Funds, U.S. Treasuries, and commodity futures.项目地址:https://gitcode.com/gh_mirrors/ya/yahoofinancials