首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从雅虎财务分析页面抓取数据

从雅虎财务分析页面抓取数据
EN

Stack Overflow用户
提问于 2022-10-09 12:20:33
回答 2查看 122关注 0票数 1

我有从雅虎金融分析页面解析数据的问题,例如https://finance.yahoo.com/quote/MSFT/analysis?p=MSFT

主要问题是此页面上的所有表都具有相同的类。我已经成功地获得了一个好的输出,但我不知道如何访问标题并将其与表行和tds匹配。

例如,第一个循环中的第一个列表(参见下面的输出)应该是第二个循环中第一个列表的值的头,等等。

不知道该怎么解决,有人有什么建议吗?

代码语言:javascript
运行
复制
    import requests
    from bs4 import BeautifulSoup

    url = f'https://finance.yahoo.com/quote/MSFT/analysis?p=MSFT'

    r = requests.get(url)
    soup = BeautifulSoup(r.content, "html5lib")


    for th in soup.find_all('table'):
        th_row = [th.text for th in th.find_all('th')]
        print(th_row)

    for tr in soup.find_all('tr'):
        td_row = [td.text for td in tr.find_all('td')]
        print(td_row)

输出

代码语言:javascript
运行
复制
FROM THE FIRST LOOP

['Earnings Estimate', 'Current Qtr. (Sep 2022)', 'Next Qtr. (Dec 2022)', 'Current Year (2023)', 'Next Year (2024)']
['Revenue Estimate', 'Current Qtr. (Sep 2022)', 'Next Qtr. (Dec 2022)', 'Current Year (2023)', 'Next Year (2024)']
['Earnings History', '9/29/2021', '12/30/2021', '3/30/2022', '6/29/2022']
['EPS Trend', 'Current Qtr. (Sep 2022)', 'Next Qtr. (Dec 2022)', 'Current Year (2023)', 'Next Year (2024)']
['EPS Revisions', 'Current Qtr. (Sep 2022)', 'Next Qtr. (Dec 2022)', 'Current Year (2023)', 'Next Year (2024)']
['Growth Estimates', 'MSFT', 'Industry', 'Sector(s)', 'S&P 500']

FROM THE SECOND LOOP

[]
['No. of Analysts', '32', '32', '43', '42']
['Avg. Estimate', '2.32', '2.58', '10.1', '11.9']
['Low Estimate', '2.25', '2.43', '9.54', '10.88']
['High Estimate', '2.43', '2.83', '10.64', '13.12']
['Year Ago EPS', '2.27', '2.48', '9.21', '10.1']
[]
['No. of Analysts', '31', '31', '46', '43']
['Avg. Estimate', '49.89B', '56.53B', '220.43B', '250.86B']
['Low Estimate', '49.27B', '53.84B', '210B', '236.22B']
['High Estimate', '51.97B', '60.38B', '235.36B', '263.42B']
['Year Ago Sales', 'N/A', 'N/A', '198.27B', '220.43B']
['Sales Growth (year/est)', 'N/A', 'N/A', '11.20%', '13.80%']
[]
['EPS Est.', '2.07', '2.31', '2.18', '2.29']
['EPS Actual', '2.27', '2.48', '2.22', '2.23']
['Difference', '0.2', '0.17', '0.04', '-0.06']
['Surprise %', '9.70%', '7.40%', '1.80%', '-2.60%']
[]
['Current Estimate', '2.32', '2.58', '10.1', '11.9']
['7 Days Ago', '2.32', '2.59', '10.12', '11.94']
['30 Days Ago', '2.32', '2.59', '10.13', '11.94']
['60 Days Ago', '2.34', '2.59', '10.34', '12.11']
['90 Days Ago', '2.49', '2.75', '10.73', '12.5']
[]
['Up Last 7 Days', 'N/A', 'N/A', 'N/A', 'N/A']
['Up Last 30 Days', '2', 'N/A', 'N/A', 'N/A']
['Down Last 7 Days', 'N/A', 'N/A', 'N/A', 'N/A']
['Down Last 30 Days', '2', '2', '3', '3']
[]
['Current Qtr.', '2.20%', 'N/A', 'N/A', 'N/A']
['Next Qtr.', '4.00%', 'N/A', 'N/A', 'N/A']
['Current Year', '9.70%', 'N/A', 'N/A', 'N/A']
['Next Year', '17.80%', 'N/A', 'N/A', 'N/A']
['Next 5 Years (per annum)', '14.96%', 'N/A', 'N/A', 'N/A']
['Past 5 Years (per annum)', '24.54%', 'N/A', 'N/A', 'N/A']
EN

Stack Overflow用户

发布于 2022-10-09 14:24:44

查看web.DataReader库。您可以使用此工具获取各种财务信息。逐行运行下面的代码示例。

代码语言:javascript
运行
复制
import pandas_datareader as web
import pandas as pd
 
df = web.DataReader('AAPL', data_source='yahoo', start='2011-01-01', end='2022-01-01')
df.head()

import yfinance as yf
aapl = yf.Ticker("AAPL")
aapl
 
 
# get stock info
aapl.info
 
# get historical market data
hist = aapl.history(period="max")
 
# show actions (dividends, splits)
aapl.actions
 
# show dividends
aapl.dividends
 
# show splits
aapl.splits
 
# show financials
aapl.financials
aapl.quarterly_financials
 
# show major holders
aapl.major_holders
 
# show institutional holders
aapl.institutional_holders
 
# show balance sheet
aapl.balance_sheet
aapl.quarterly_balance_sheet
 
# show cashflow
aapl.cashflow
aapl.quarterly_cashflow
 
# show earnings
aapl.earnings
aapl.quarterly_earnings
 
# show sustainability
aapl.sustainability
 
# show analysts recommendations
aapl.recommendations
 
# show next event (earnings, etc)
aapl.calendar
 
# show ISIN code - *experimental*
# ISIN = International Securities Identification Number
aapl.isin
 
# show options expirations
aapl.options
 
# get option chain for specific expiration
opt = aapl.option_chain('YYYY-MM-DD')
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74004699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档