📜  使用Python多线程下载雅虎股票历史 – yfinance

📅  最后修改于: 2022-05-13 01:54:25.705000             🧑  作者: Mango

使用Python多线程下载雅虎股票历史 – yfinance

Yfinance是一个Python包,它使我们能够以 Pythonic 的方式从 Yahoo Finance API 获取历史市场数据。在 yfinance 的帮助下,所有Python开发人员都可以轻松获取数据。

我们可以很容易地从 yfinance 下载历史股票数据,但问题是,它非常耗时。因此,我们使用多线程来掩盖时间。多线程使我们能够通过并发执行多个线程来下载大量数据。

安装

这个模块没有内置于Python。要安装它,请在终端中键入以下命令。

pip install yfinance

让我们一步一步地看一下这个过程,下面通过实现来解释:

第 1 步:获取所有必需的模块



Python3
import yfinance as yf


Python
# Get the stocks info
import yfinance as yf
 
# Ticker is a function responsible
# for fetching the data MSFT is
# representing info about Microsoft
# Corporation
msft = yf.Ticker('MSFT')
 
# msft.info will return all information
# about microsoft corporation
data = msft.history()
 
# printing the data
print(data)


Python3
# pip install yfinance
import yfinance as yf
 
# Here we use yf.download function
data = yf.download(
 
    # tickers has value of company
    # shortname
    tickers='MSFT',
)
 
# printing the data
print(data)


Python3
# pip install yfinance
import yfinance as yf
 
ticker_list = ['IBM', 'MSFT', 'AAPL', 'AMZN']
 
# Here we use yf.download function
data = yf.download(
     
    # passes the ticker
    tickers=ticker_list,
     
    # used for access data[ticker]
    group_by='ticker',
 
)
 
# used for making transpose
data = data.T
 
for t in ticker_list:
   
    # printing name
    print(t)
    print('\n')
     
    # used data.loc as it takes only index
    # labels and returns dataframe
    print(data.loc[t]) 
    print('\n')


Python
# pip install yfinance
import yfinance as yf
import time
 
# Time starts from here
start = time.time()
 
ticker_list = ['IBM', 'MSFT', 'AAPL', 'AMZN']
 
# Here we use yf.download function
data = yf.download(
    tickers=ticker_list,
    threads=True,
    group_by='ticker',
 
)
 
# used for making transpose
data = data.T
 
for t in ticker_list:
    print(t)
     
    # used data.loc as it takes only index
    # labels and returns dataframe
    print(data.loc[t]) 
 
# Total time calculated
print('The program takes ', time.time()-start, 'seconds.')


第二步:获取股票历史数据

Python

# Get the stocks info
import yfinance as yf
 
# Ticker is a function responsible
# for fetching the data MSFT is
# representing info about Microsoft
# Corporation
msft = yf.Ticker('MSFT')
 
# msft.info will return all information
# about microsoft corporation
data = msft.history()
 
# printing the data
print(data)

输出:



这是微软公司最近一年的股票数据。请注意,如果我们使用data = msft.history(period='max')它将返回到目前为止的所有数据。

第 3 步:下载数据

蟒蛇3

# pip install yfinance
import yfinance as yf
 
# Here we use yf.download function
data = yf.download(
 
    # tickers has value of company
    # shortname
    tickers='MSFT',
)
 
# printing the data
print(data)

输出:

因此,1 of 1 下载完成。只是一家公司从开始到结束到现在的所有数据。

第四步:从多家公司下载数据

这是用于从 IBM、Apple (AAPL)、Amazon (AMZN)、Microsoft (MSFT) 等公司下载数据的代码。

蟒蛇3

# pip install yfinance
import yfinance as yf
 
ticker_list = ['IBM', 'MSFT', 'AAPL', 'AMZN']
 
# Here we use yf.download function
data = yf.download(
     
    # passes the ticker
    tickers=ticker_list,
     
    # used for access data[ticker]
    group_by='ticker',
 
)
 
# used for making transpose
data = data.T
 
for t in ticker_list:
   
    # printing name
    print(t)
    print('\n')
     
    # used data.loc as it takes only index
    # labels and returns dataframe
    print(data.loc[t]) 
    print('\n')



输出:

第五步:计算执行时间和使用多线程

由于 yfinance 使用其自己的内置线程技术进行大量下载。为此,我们需要在yf.download 中分配一个新参数

计算时间的程序,我们使用时间模块

Python

# pip install yfinance
import yfinance as yf
import time
 
# Time starts from here
start = time.time()
 
ticker_list = ['IBM', 'MSFT', 'AAPL', 'AMZN']
 
# Here we use yf.download function
data = yf.download(
    tickers=ticker_list,
    threads=True,
    group_by='ticker',
 
)
 
# used for making transpose
data = data.T
 
for t in ticker_list:
    print(t)
     
    # used data.loc as it takes only index
    # labels and returns dataframe
    print(data.loc[t]) 
 
# Total time calculated
print('The program takes ', time.time()-start, 'seconds.')

线程前:

线程后: