【问题标题】:Downloading multiple tickers data from Yahoo Finance从 Yahoo Finance 下载多个代码数据
【发布时间】:2022-02-16 18:00:18
【问题描述】:

我看到了 Andrej Kesely 在 StackOverflow 上发布的这段代码。我真的觉得它很有帮助,但我正在尝试下载多个代码而不是一个代码。 Cannot scrape from table in 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))

【问题讨论】:

    标签: python yahoo-finance


    【解决方案1】:

    为什么不使用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 
    

    【讨论】:

    【解决方案2】:

    你也可以试试 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')
    

    【讨论】:

      【解决方案3】:

      您应该寻找:

      在这种情况下,您可以使用 f-string:

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

      【讨论】:

      猜你喜欢
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-23
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      相关资源
      最近更新 更多