簡體   English   中英

從 Yahoo Finance 下載多個代碼數據

[英]Downloading multiple tickers data from Yahoo Finance

我看到了 Andrej Kesely 在 StackOverflow 上發布的這段代碼。 我真的覺得它很有幫助,但我正在嘗試下載多個代碼而不是一個代碼。 無法從 yahoo finance 中的表中刪除

目前,我正在下載多個價格行情,我使用 ['AAPL', 'MSFT', 'AMZN'],所以我試圖改變

url = ["https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL","https://finance.yahoo.com/quote/AAPL/key-statistics?p=MSFT" ]

它沒有用,我想知道是否有人知道如何將它更改為 url。非常感謝。

import requests
from bs4 import BeautifulSoup


url = "https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL"

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0"
}

soup = BeautifulSoup(requests.get(url, headers=headers).content, "html.parser")

for t in soup.select("table"):
    for tr in t.select("tr:has(td)"):
        for sup in tr.select("sup"):
            sup.extract()
        tds = [td.get_text(strip=True) for td in tr.select("td")]
        if len(tds) == 2:
            print("{:<50} {}".format(*tds))

為什么不使用DataReader

from pandas_datareader.data import DataReader # conda install pandas-datareader
data = DataReader(['AAPL', 'AMZN', 'GOOG', 'MFST'], 'yahoo', start='2015-01-01', end='2021-08-30')
print(data)

結果:

Attributes   Adj Close                                            Close  \
Symbols           AAPL         AMZN         GOOG       MFST        AAPL   
Date                                                                      
2015-01-02   24.782110   308.519989   523.373108  22.000000   27.332500   
2015-01-05   24.083958   302.190002   512.463013  23.700001   26.562500   
2015-01-06   24.086227   295.290009   500.585632  23.700001   26.565001   
2015-01-07   24.423975   298.420013   499.727997  21.100000   26.937500   
2015-01-08   25.362394   300.459991   501.303680  21.100000   27.972500   
...                ...          ...          ...        ...         ...   
2021-08-23  149.710007  3265.870117  2821.989990   0.000800  149.710007   
2021-08-24  149.619995  3305.780029  2847.969971   0.000700  149.619995   
2021-08-25  148.360001  3299.179932  2859.000000   0.000800  148.360001   
2021-08-26  147.539993  3316.000000  2842.459961   0.000500  147.539993   
2021-08-27  148.600006  3349.629883  2891.010010   0.000400  148.600006 

您也可以嘗試 yfinace:

import yfinance as yf
etf = ['AXP','AAPL','BA','CAT','CSCO','CVX','XOM','GS','HD','IBM','INTC','JNJ','KO'] #and any tickers you'd add to be retrived
tit = yf.download(tickers=etf, period='max')

你應該尋找:

在這種情況下,您可以使用 f 字符串:


tickers = ['AAPL', 'MSFT', 'AMZN']
for tick in tickers:
    print(f"https://finance.yahoo.com/quote/{tick}/key-statistics?p={tick}")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM