随笔 - 148  文章 - 3  评论 - 2  阅读 - 11万

Python3 获取 yahoo 数据

------------------ 方法 一 -----------------

1. 安装 pandas_datareader

pip install pandas_datareader

2. 获取对应股票的信息

import pandas_datareader as pdr

pdr.get_data_yahoo(‘AAPL’)

 

原文地址:https://blog.csdn.net/jinruoyanxu/article/details/79154224

 

import pandas.io.data as web

提示警告,pandas.io.data 已经 转移到了一个单独的包里面了,于是 按照提示 安装了  pandas_datareader,方法是在cmd环境下 pip install pandas_datareader,然后在命令行里面输入 下面那行就ok了

import pandas_datareader.data as web 


pandas_datareader是一个远程获取金融数据的Python工具,通过它可以方便获得下面公司和机构的数据:

Yahoo! Finance//雅虎金融

Google Finance//谷歌金融

Enigma//Enigma是一个公共数据搜索的提供商

St.Louis FED (FRED)//圣路易斯联邦储备银行

Kenneth French’s data library//肯尼斯弗兰奇资料库

World Bank//世界银行

OECD//经合组织

Eurostat//欧盟统计局

Thrift Savings Plan//美国联邦政府管理离退休的组织

Oanda currency historical rate  //外汇经纪商

Nasdaq Trader symbol definitions //纳斯达克

pandas-datareader安装非常简单:

$ pip install pandas-datareader

使用说明:

0.19.0开始, pandas不支持以前的pandas.io.data或pandas.io.wb包,如果你看见网上和书里使用 import pandas.io.data as web

你需要替换pandas.io为pandas_datareader。

如:

from pandas.io import data, wb # becomes

from pandas_datareader import data, wb

我们使用pandas_datareader获得苹果股票信息:

import pandas_datareader as pdr

pdr.get_data_yahoo(‘AAPL’)

当然你也可以换成你喜欢的股票。

 

------------------ 方法 二 -----------------

1. 安装 fix_yahoo_finance

 pip install fix_yahoo_finance

 

2. Python 调用

复制代码
import pandas_datareader.data as web
import fix_yahoo_finance as fy

#start = datetime.datetime(2010,1,1)
#end = datetime.datetime(2017,1,1)

fy.pdr_override()

#df = web.get_data_yahoo('SPY','yahoo','20100101', '20140101')

all_data = {}
for ticker in ['AAPL', 'GOOG']:
    all_data[ticker] = web.get_data_yahoo(ticker, '2010-1-1', '2010-2-1')

print(all_data['AAPL'])
复制代码

 

 

 

另外,查看 https://pypi.org/project/fix-yahoo-finance/

Requirements

Quick Start

from pandas_datareader import data as pdr

import fix_yahoo_finance as yf
yf.pdr_override() # <== that's all it takes :-)

# download dataframe
data = pdr.get_data_yahoo("SPY", start="2017-01-01", end="2017-04-30")

# download Panel
data = pdr.get_data_yahoo(["SPY", "IWM"], start="2017-01-01", end="2017-04-30")

I’ve also added some options to make life easier :)

Below is the full list of acceptable parameters:

data = pdr.get_data_yahoo(
            # tickers list (single tickers accepts a string as well)
            tickers = ["SPY", "IWM", "..."],

            # start date (YYYY-MM-DD / datetime.datetime object)
            # (optional, defaults is 1950-01-01)
            start = "2017-01-01",

            # end date (YYYY-MM-DD / datetime.datetime object)
            # (optional, defaults is Today)
            end = "2017-04-30",

            # return a multi-index dataframe
            # (optional, default is Panel, which is deprecated)
            as_panel = False,

            # group by ticker (to access via data['SPY'])
            # (optional, default is 'column')
            group_by = 'ticker',

            # adjust all OHLC automatically
            # (optional, default is False)
            auto_adjust = True,

            # download dividend + stock splits data
            # (optional, default is None)
            # options are:
            #   - True (returns history + actions)
            #   - 'only' (actions only)
            actions = True,

            # How may threads to use?
            threads = 10
        )

It can also be used as a stand-alone library (without pandas_datareader) if you want:

import fix_yahoo_finance as yf
data = yf.download("SPY", start="2017-01-01", end="2017-04-30")

Installation

Install fix_yahoo_finance using pip:

$ pip install fix_yahoo_finance --upgrade --no-cache-dir

Requirements

posted on   bruce_he  阅读(3747)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示

目录导航