1

python 'yfinance' 模块在 pandas 数据框中下载许多金融证券的报价,同时在控制台中显示进度条。这样:

import yfinance as yf
Tickerlist = ["AAPL","GOOG","MSFT"]
quote = yf.download(tickers=Tickerlist,period='max',interval='1d',group_by='ticker')

我想实时捕捉控制台进度条,代码应该是这样的:

import sys
import subprocesss
process = subprocess.Popen(["yf.download","tickers=Tickerlist","period='max'","interval='1d'","group_by='ticker'"],stdout=quote)
while True:
    out = process.stdout.read(1)
    sys.stdout.write(out)
    sys.stdout.flush()

我把子进程弄得一团糟。我需要你的帮助!谢谢。我已经看到了处理这个主题的所有链接,但无法解决我的问题。

4

1 回答 1

1

你需要两个 python 文件来做你想做的事。

第一个是 yf_download.py,第二个是 run.py

文件代码如下所示,可以通过 run.py 运行

蟒蛇运行.py

yf_download.py

import sys
import yfinance as yf

Tickerlist = ["AAPL","GOOG","MSFT"]

def run(period):
    yf.download(tickers=Tickerlist, period=period,interval='1d',group_by='ticker')

if __name__ == '__main__':
    period = sys.argv[1]
    run(period)

运行.py

import sys
import subprocess

process = subprocess.Popen(["python", "yf_download.py", "max"],stdout=subprocess.PIPE)

while True:
    out = process.stdout.read(1)
    if process.poll() is not None:
        break
    if out != '':
        sys.stdout.buffer.write(out)
        sys.stdout.flush()

于 2019-12-23T14:39:37.903 回答