我看到Andrej Kesely在StackOverflow上发布的这段代码。我真的发现它很有帮助,但我正试图下载多个股票而不是一个股票。
目前,我正在下载多个价格报价器,我使用['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
你也可以试试yfinance:
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')
您应该查找:
- https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT
- https://finance.yahoo.com/quote/AMZN/key-statistics?p=AMZN
- https://finance.yahoo.com/quote/APPL/key-statistics?p=APPL
在这种情况下,可以使用f字符串:
tickers = ['AAPL', 'MSFT', 'AMZN']
for tick in tickers:
print(f"https://finance.yahoo.com/quote/{tick}/key-statistics?p={tick}")